qff*_*qff 5 javascript unit-testing mocha.js pytest
在pytest中,您可以设置可以具有多个不同值的灯具。这些被称为“参数化夹具”。将使用这些灯具的所有可能值组合来运行使用这些灯具的测试。
例
# 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
Run Code Online (Sandbox Code Playgroud)
在这里,该功能test_sum将总共运行四次。:每次运行将使用不同的参数a=1, b=3,a=1, b=4,a=2, b=3,和a=2, b=4分别。
题
在任何Javascript测试库中,是否有等效于参数化的灯具?(我们目前使用的是摩卡咖啡,因此这对我们来说将是最有趣的)
Jest 现在将该实用程序合并到其代码库中:)它位于it.each/test.each下。对于旧版本的 jest,您可以使用下面提到的库之一。
最近,我发现有一个 Jest 实用程序,称为jest-each或语法不太好的jest-in-case,这是pytest.mark.parametrized.
很不幸的是,不行。从我在互联网上找到的信息来看,Mocha 即使在今天也不支持它。对于这种语法也有被拒绝的提案,但目前,唯一的解决方案是他们所谓的动态生成测试,语法如下所示(取自文档)。此外,您还可以阅读有关JS 与 Python 测试的悲惨状态的更多信息。
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)
})
})
})Run Code Online (Sandbox Code Playgroud)