【问题标题】:Jest looping through dynamic test casesJest 循环遍历动态测试用例
【发布时间】:2018-01-24 14:51:32
【问题描述】:

如何在 Jest 中循环遍历动态测试用例?

我有如下测试用例,如何使用it/test 方法动态创建 jest 测试用例。

这是我尝试过的,但是它只是通过而没有执行循环中的测试用例。

    const mymodule = require('mymodule');

    const testCases = [
        {q: [2, 3],r: 5},
        {q: [1, 2],r: 3},
        {q: [7, 0],r: 7},
        {q: [4, 4],r: 8}
    ];

    describe("Test my Math module", () => {
        test("test add method", () => {
            for (let i = 0; i < testCases.length; i++) {
                const { q,r } = testCases[i];
                it(`should  add ${q[0]},${q[1]} to ${expected}`, () => {
                    const actual = mymodule.add(q[0] + q[1]);
                    expect(actual).toBe(expected);
                });
            }
        });
    
    });

【问题讨论】:

    标签: javascript node.js unit-testing jestjs


    【解决方案1】:

    有一种内置方法可以做到这一点:test.each(table)(name, fn, timeout)

    例如

    test.each([[1, 1, 2], [1, 2, 3], [2, 1, 3]])(
      '.add(%i, %i)',
      (a, b, expected) => {
        expect(a + b).toBe(expected);
      },
    );
    

    二维数组中的每个内部数组都作为参数传递给测试函数。

    【讨论】:

    • @nickang 是的,它很有用。如果可以将被测输出呈现为字符串,我还发现使用 expect(result).toMatchInlineSnapshot(&lt;string&gt;) 很有用(这不必是 JSX,例如,您可以呈现 JSON 数据结构并对其进行快照匹配)。
    【解决方案2】:

    如果一个人通过,他们都会。因此,您只需要一个进行阳性测试。理想情况下,您希望 2 个数字等于总和的 1 个正数,以及您传递字符串或其他内容并引发错误的失败。

    TL/DR;你不需要 4 个测试都做同样的事情。此外,您需要将循环测试用例从父测试中拉出以使循环正常工作。

    这会很好用:

    import jest from 'jest';
    import { sumModule } from './';
    
    const tests = [
      {x: 1, y: 2, r: 3}, 
      {x: 3, y: 4, r: 7}
    ];
    
    describe('Adding method', () => { 
        for(let i = 0; i < tests.length; i++){
          it('should add its params', () => {
            const actual = sumModule(tests[i].x, tests[i].y);
            expect(actual).toBe(tests[i].r);
          });
        }
    });
    

    【讨论】:

    • 不幸的是,当循环通过异步操作生成的动态测试用例时,这是不可能的。 :(github.com/facebook/jest/issues/1619#issuecomment-358576732
    • 是的。迭代异步配置时不起作用,与方法无关。 :(
    • 我在beforeAll中有一个异步代码块,我想根据beforeAll中代码块的结果生成测试用例。它继续写Your test suite must contain at least one test.
    【解决方案3】:

    如果有人想知道这如何适用于单个输入函数,这是一个示例

    const testData = [
          ['input1', 'output1'],
          ['input2', 'output2'],
          ['input3', 'output3'],
    ]
    
    test.each(testData)('myFunc work correctly for %s',(input, output) =>{
            expect(yourFunc(input)).toBe(output)
    })
    

    https://jestjs.io/docs/en/api#testeachtablename-fn-timeout

    【讨论】:

      【解决方案4】:

      你可以使用经典的 JS,它更易读,代码更少:

      [
        { input: [2, 3], output: 5 },
        { input: [1, 2], output: 3 },
      ].forEach(({ input, output }) => {
        it(`works correctly for ${input}`, () => {
          // ...
      
          expect(...).toBe(output);
        });
      })
      

      【讨论】:

      • 这应该是答案。它更易于编写、阅读和使用。能够对测试名称进行用户插值也是一个巨大的优势,足以让我不使用 test.each。
      【解决方案5】:

      for循环是开玩笑的,不需要无缘无故使用内置函数!

       test('adding positive numbers is not zero', () => {
        for (let a = 1; a < 10; a++) {
          for (let b = 1; b < 10; b++) {
            expect(a + b).not.toBe(0);
          }
        }
      });
      

      【讨论】:

        猜你喜欢
        • 2021-12-16
        • 2021-12-24
        • 2020-03-30
        • 1970-01-01
        • 1970-01-01
        • 2018-09-26
        • 1970-01-01
        • 1970-01-01
        • 2013-12-26
        相关资源
        最近更新 更多