gpt4 book ai didi

javascript - 是否有等同于 pytest 的参数化固定装置的 Javascript?

转载 作者:搜寻专家 更新时间:2023-11-01 04:35:03 25 4
gpt4 key购买 nike

在 pytest 中,您可以设置可以具有多个不同值的固定装置。这些被称为“参数化 fixture ”。使用这些固定装置的测试将使用来自这些固定装置的所有可能值组合运行。

示例

# Fixture `a` can have the values `1` and `2`
@pytest.fixture(params=[1, 2])
def a(request):
yield request.param

# Fixture `b` can have the values `3` and `4`
@pytest.fixture(params=[3, 4])
def b(request):
yield request.param

# The test `test_sum` uses the fixtures `a` and `b`
def test_sum(a, b):
assert sum([a, b]) == a + b

这里,test_sum 函数将总共运行四次。每次运行将使用不同的参数:a=1, b=3, a=1, b=4, a=2, b=3, 和 a=2, b=4 分别。

问题

是否有与任何 Javascript 测试库中的参数化固定装置等效的东西? (我们目前使用 mocha,所以这对我们来说是最有趣的)

最佳答案

Jest 现在将该实用程序合并到其代码库中 :) 它在 it.each/test.each 下.对于旧版本的 jest,您可以使用下面提到的库之一。

旧答案:

最近,我发现有一个名为 jest-each 的 Jest 实用程序或者使用不太好的语法 jest-in-case这是 pytest.mark.parametrized 的一个很好的替代品。

下面是旧的原始答案:

不幸的是没有。从我在互联网上发现的信息来看, Mocha 直到今天也不支持它。还有rejected proposal(s)对于这样的语法,但目前,唯一的解决方案是他们称之为 dynamically generating tests 的东西语法类似于下面的代码(取自文档)。此外,您还可以阅读更多关于 sad state of JS vs. Python testing 的信息.

describe('Possible user names behaves correctly ', () => {
const TEST_CASES = [
{args: ['rj'], expected: false},
{args: ['rj12345'], expected: false},
{args: ['rj123'], expected: true},
]

TEST_CASES.forEach((testCase) => {
it(`check user name ${JSON.stringify(testCase.args)}`, () => {
const result = checkUserName.apply(this, testCase.args)

expect(testCase.expected).toEqual(result)
})
})
})

关于javascript - 是否有等同于 pytest 的参数化固定装置的 Javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47692421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com