admin管理员组文章数量:1302379
In pytest you can set up fixtures that can have multiple different values. These are called "parameterized fixtures". A test that uses these fixtures will be run with all possible binations of values from those fixtures.
Example
# 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
Here, the function test_sum
will be run four times in total. Each run will use different arguments: a=1, b=3
, a=1, b=4
, a=2, b=3
, and a=2, b=4
respectively.
Question
Is there an equivalent to parametrized fixtures in any Javascript testing library? (We currently use mocha, so that would be the most interesting to us)
In pytest you can set up fixtures that can have multiple different values. These are called "parameterized fixtures". A test that uses these fixtures will be run with all possible binations of values from those fixtures.
Example
# 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
Here, the function test_sum
will be run four times in total. Each run will use different arguments: a=1, b=3
, a=1, b=4
, a=2, b=3
, and a=2, b=4
respectively.
Question
Is there an equivalent to parametrized fixtures in any Javascript testing library? (We currently use mocha, so that would be the most interesting to us)
Share Improve this question edited Dec 7, 2017 at 10:19 qff asked Dec 7, 2017 at 10:04 qffqff 5,9623 gold badges43 silver badges65 bronze badges 1- 2 I'm looking for the same stuff. Surprisely, pytest's fixture alike feature has been requested for years. However, these JS testing frontiers never really understand how elegant the tests can be when with builtin fixture. – Tim Wu Commented Mar 23, 2018 at 23:34
3 Answers
Reset to default 4Jest now incorporated the utility into its codebase :) It's under it.each
/test.each
. For older versions of jest, you can use one of the libraries mentioned below.
Old answer:
Recently, I discovered there is a utility for Jest called jest-each or with less nice syntax jest-in-case which is quite a good alternative to pytest.mark.parametrized
.
Old old original answer below:
Unfortunately no. Mocha does not support it even today from what I found on the internet. There is also rejected proposal(s) for such syntax, but currently, the only solution is something that they call dynamically generating tests and the syntax looks like in the code below (taken from the doc). Also, you can read more about 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)
})
})
})
Having encountered the need for parameterized tests for both python and js, I think the one that is both easy to use and readable is the .each
syntax included in Jest (sorry, haven't found options for Mocha!).
A good example can be found here: https://dev.to/flyingdot/data-driven-unit-tests-with-jest-26bh
Playwright Test testing framework borrows this concept from PyTest; see https://playwright.dev/docs/test-fixtures for details.
本文标签: unit testingIs there a Javascript equivalent to pytest39s parameterized fixturesStack Overflow
版权声明:本文标题:unit testing - Is there a Javascript equivalent to pytest's parameterized fixtures? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741702215a2393357.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论