1

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]
Arsen
  • 23
  • 6

1 Answers1

1

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
Ben Horsburgh
  • 563
  • 4
  • 10
  • Thanks! But how can I do it using fixtures? Let say I have a dictionary which I use as a config for tests `config = [ { 'param1': 'param1-val1', 'param2': [ 'param2-val1', 'param2-val2' ] }, { 'param1': 'param1-val2', 'param2': [ 'param2-val3', 'param2-val4' ] } ]` How can I parametrize TestThreads suite with `param1` while `test_b` in it with `param2`? – Arsen Dec 29 '21 at 14:24
  • Your config here is a list, how are you meaning to use this? One option is that you could create an auto-use fixture, and then parameterise the key to use. I'm not sure I fully understand your comment though. – Ben Horsburgh Dec 29 '21 at 14:32
  • I meant list of dictionaries, sorry. – Arsen Dec 29 '21 at 14:35
  • I think you may be confusing the role of fixtures and parameterise. Fixtures are for providing background, constant, environmental things, a bit like "fittings and fixtures" in a house. Parameterise is where you can specify things which are specific to an individual test. Is your config confusing both of these? If feels like a parameterised list of fixtures (which is not a clean design) – Ben Horsburgh Dec 29 '21 at 14:37
  • @BenHorsburgh it is not the case when class param is scoped as "class". Asked a question related to a similar issue, what do you think? [Link](https://stackoverflow.com/questions/73235003/cyclic-parametrized-test-in-a-class-with-class-scoped-parameters) – igor Aug 07 '22 at 06:45