Lost_DM
Lost_DM

Reputation: 951

py.test run tests in specific testSuite

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:

    for test_parameters in params:
        parameterized_test_suite.addTest(ParametrizedTestCase.parametrize(TestCaseWithParameters,param=test_parameters))

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

Answers (1)

flub
flub

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

Related Questions