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











up vote
3
down vote

favorite
1












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)










share|improve this question




















  • 1




    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
    Mar 23 at 23:34















up vote
3
down vote

favorite
1












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)










share|improve this question




















  • 1




    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
    Mar 23 at 23:34













up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





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)










share|improve this question















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)







javascript unit-testing mocha pytest






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 7 '17 at 10:19

























asked Dec 7 '17 at 10:04









qff

2,68421645




2,68421645








  • 1




    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
    Mar 23 at 23:34














  • 1




    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
    Mar 23 at 23:34








1




1




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
Mar 23 at 23:34




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
Mar 23 at 23:34












1 Answer
1






active

oldest

votes

















up vote
1
down vote













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 what 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)
})
})
})








share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47692421%2fis-there-a-javascript-equivalent-to-pytests-parameterized-fixtures%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    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 what 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)
    })
    })
    })








    share|improve this answer



























      up vote
      1
      down vote













      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 what 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)
      })
      })
      })








      share|improve this answer

























        up vote
        1
        down vote










        up vote
        1
        down vote









        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 what 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)
        })
        })
        })








        share|improve this answer














        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 what 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)
        })
        })
        })








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





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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 19 at 11:35

























        answered Nov 19 at 11:30









        miso.belica

        1,36311313




        1,36311313






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47692421%2fis-there-a-javascript-equivalent-to-pytests-parameterized-fixtures%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith