开玩笑地循环通过动态测试用例

梅哈里(Mehari):

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

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

这是我尝试过的方法,但是它只是通过而不会影响循环中的测试用例。

const mymodule = require('mymodule');
    let 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 sub module", () => {
            for (let i = 0; i < testCases.length; i++) {
                let item = testCases[i];
                let q = item.q;
                let expected = item.r;
                it(`should  add ${q[0]},${q[1]} to ${expected}`, () => {
                    let actual = mymodule.add(q[0] + q[1]);
                    expect(actual).toBe(expected);
                });
            }
        });

    });
贾斯汀:

有一种内置的方法可以做到这一点:

https://jestjs.io/docs/zh-CN/api#1-testeachtable-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);
  },
);

其中2D数组中的每个内部数组都作为args传递给测试函数。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章