2

I'm using pytest for unittests and I would like to parametrize the tests. I know I can use @pytest.mark.parametrize at class level and at method level.

@pytest.mark.parametrize("param1", [1,2,3,4,5])
class TestXYZ:
    @pytest.mark.parametrize("param2", [6,7,8,9])
    def test_xyz(self, param1, param2):
        assert param1 == param2

However, in my case it happens that the param2 list depends on the actual param1 value. I would like to obtain sth like this:

@pytest.mark.parametrize("param1", [1,2,3,4,5])
class TestXYZ:
    @pytest.mark.parametrize("param2", getparams(param1))
    def test_xyz(self, param1, param2):
        assert param1 == param2

However, i could not figure out how to do it.

I tried to use nested functions, but this did'nt work out, too:

@pytest.mark.parametrize("param1", [1,2,3,4,5])
class TestXYZ:
    def test_gen(self, param1):
        @pytest.mark.parametrize("param2", [6,7,8,9])
        def test_xyz(self, param1, param2):
            assert param1 == param2

        return test_gen

Pytest then just collect a testcase for param1.

1 Answers1

0

This should work for you:

import pytest

parameter1 = [1, 2, 3]

@pytest.mark.parametrize("param1", parameter1)
class TestXYZ:
    @pytest.mark.parametrize("param2", [4, 3, parameter1[0]])
    def test_xyz(self, param1, param2):
        assert param1 == param2

With output: 7 failed, 2 passed for this example.

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
  • Sorry, but this does not answer my question. My problem was that the list of param2 values depends on the actual selected value for param1. – Wör Du Schnaffzig Jan 21 '22 at 09:04