How to parametrize class and function separately in pytest

1.9k Views Asked by At

I have a test class with 2 tests. How can I parametrize whole class while having one test parametrized additionally?

I need test_b executed 1 time for param0 and 2 times for param1

Module threads.py
  Class TestThreads
     Function test_a[param0]
     Function test_b[param0-0]
     Function test_a[param1]
     Function test_b[param1-0]
     Function test_b[param1-1]
1

There are 1 best solutions below

5
On

You can parameterise both class and methods individually and they stack together. For example, to get the outcome you describe, you can parameterise the class with param0, and parameterise test_b with param1:

import pytest


@pytest.mark.parametrize("param0", [0])
class TestThreads:

    def test_a(self, param0):
        assert True

    @pytest.mark.parametrize("param1", [0, 1])
    def test_b(self, param0, param1):
        assert True