Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Javascript equivalent to pytest's parameterized fixtures?

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 combinations 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)

like image 350
qff Avatar asked Dec 07 '17 10:12

qff


People also ask

Can pytest fixtures be parameterized?

pytest. fixture() allows one to parametrize fixture functions.

What is pytest Parametrize?

The @pytest. mark. parametrize() decorator lets you parameterize arguments of the testing function independent of fixtures you created. Now I can pass a list of invalid arguments and use pytest. raises(AssertionError) to assert that the invalid terms result in the expected exception.

What is fixture JS?

Fixtures is a fancy word for sample data. Fixtures allow you to populate your testing database with predefined data before your tests run. Fixtures are database independent and written in YAML. There is one file per model.

What is the syntax to run a parameterized test in pytest?

you can put @pytest. mark. parametrize style parametrization on the test functions to parametrize input/output values as well.


1 Answers

Jest 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)
    })
  })
})
like image 184
Mišo Avatar answered Oct 17 '22 07:10

Mišo