Sobes.tech
Senior

How many times will test_balance be run with parametrize on the class and on the test function?

sobes.tech AI

Answer from AI

If @pytest.mark.parametrize is used in pytest both at the class level and at the test function level, then the total number of test runs is the product of the number of parameters at the class level and the number of parameters at the function level.

For example, if the class is annotated as:

@pytest.mark.parametrize("x", [1, 2])
class TestClass:
    @pytest.mark.parametrize("y", [3, 4])
    def test_balance(self, x, y):
        pass

Then test_balance will be run 2 (for x) * 2 (for y) = 4 times with the combinations:

  • x=1, y=3
  • x=1, y=4
  • x=2, y=3
  • x=2, y=4

Thus, parameterization at the class and function levels is combined, creating a Cartesian product of the parameters.

This allows for convenient creation of tests with many input data combinations.