【发布时间】:2018-05-21 09:42:35
【问题描述】:
在 pytest 中,您可以设置可以具有多个不同值的固定装置。这些被称为“参数化夹具”。使用这些fixture 的测试将使用这些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,所以这对我们来说是最有趣的)
【问题讨论】:
-
我正在寻找同样的东西。令人惊讶的是,pytest 的fixture 相似功能是requested for years。然而,这些 JS 测试前沿从来没有真正理解使用内置夹具时测试的优雅程度。
标签: javascript unit-testing mocha.js pytest