Reputation: 951
I'm new to py.test. So far I like what I see and want to integrate it to our CI process.
Currently we use a different kind of parameterization scheme for our tests which I will explain briefly:
params
is a touple of of of tuples, each representing different set of parameters.TestCaseWithParameters
which is a unittest.TestCase
class. Something like this:for test_parameters in params: parameterized_test_suite.addTest(ParametrizedTestCase.parametrize(TestCaseWithParameters,param=test_parameters))
self.params
and runs all tests functions it with those different params.params
and TestSomethingWithParameters
has dozens of tests, there are a lot of tests in total.My question: How would I go about translating this to py.test?
I've read this article about the pytest_generate_tests
hook, but it seems it injects dependency per test function, and I need it per TestCase...
The simplest way would be to tell py.test to run the specific parameterized_tes_suite
I create already, but I did not find a way to do so...
A different way would be to do a similar dependency-injection at TestCase class level, but I have not found a way to do that either.
Upvotes: 2
Views: 1657
Reputation: 6357
You can easily parametrize whole classes using the @pytest.mark.parametrize
marker:
import pytest
@pytest.mark.parametrize('n', [0, 1])
class TestFoo:
def test_42(self, n):
assert n == 42
def test_7(self, n):
assert n == 7
See the documentation on the parameterize marker for details on how to pass in multiple arguments etc. And also have a look at how to apply markers to classes and modules for more information on this.
Upvotes: 2