pytest class with different parameters #9290
-
Hi all, I have a question regarding using pytest with test classes. I wrote such a testclass which basically looks as follows:
Everything works as intended. But now I want to do the following: chosen_if shall not be hardcoded but the whole class shall run with two if both connected to my DUT:
Is this possibly anyhow? I'm quite new in pytest and I googled for hours but only found solutions which can be used for the test_xxx() functions itself but not for the whole class. So far, chosen_if is hardcoded to "SPI". If I want to test the second_if (I2C) I have to change in the source code. Later on the test shall run on Jenkins so this is not a solution at all. :) Is there a good way to do what I want? Kind regards! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The A more pytest-like and powerful mechanism to achieve the same goal are pytest's fixtures - they'll take a bit of time to wrap your head around, but are totally worth it! Your code could perhaps look like this (with a couple of assumption, your original code has various issues): class Test_HW:
@pytest.fixture(autouse=True)
def interface(self):
# before every test
init_if(chosen_if)
yield
# after every test
deinit_if(chosen_if)
def test_nr1(self):
# do anything
def test_nr2(self):
# do anything such a fixture can then be parametrized: class Test_HW:
@pytest.fixture(autouse=True, params=['i2c', 'spi'])
def interface(self, request):
# before every test
init_if(request.param) # 'i2c' or 'spi'
yield
# after every test
deinit_if(request.param)
def test_nr1(self):
# do anything
def test_nr2(self):
# do anything |
Beta Was this translation helpful? Give feedback.
The
setup_class
andteardown_class
methods you describe are actually from pytest's support of unittest/nose, and not the "native" way of doing this with pytest.A more pytest-like and powerful mechanism to achieve the same goal are pytest's fixtures - they'll take a bit of time to wrap your head around, but are totally worth it!
Your code could perhaps look like this (with a couple of assumption, your original code has various issues):