Paz Bazak
Paz Bazak

Reputation: 63

How to run all pytest tests with extra function calls using class

I have a base class called BaseTest which all my test cases are inheriting from.

Each TestCase have X tests and a method - different_configuration()

Instead of creating test_xyz and test_xyz_different for each test (which means to double my tests in the code, and even more if I'll have x different configurations)

Is there a way to make the base class have something or even a fixture which per existing test will create/run another test but will call self.different_configuration() at the beginning.

EXTRA INFO - I have a dict as an attribute which has configurations and, there's one configuration, which for each test I'd like to test both available options.

self.different_configuration() just changes the configuration value before the test itself.

Illustration -

    def test_x(self):
        self.run_and_verify()

    def test_x_different_configuration(self):
        self.different_configurations()
        self.run_and_verify()

So for each existing test I create another test with this function call at the beginning, isn't there a cleaner way to achieve it?

Upvotes: 0

Views: 951

Answers (1)

Marco.S
Marco.S

Reputation: 433

You can parametrize a fixture.

You will have to extract your configuration values from the dictionary to a list, though, to use it on the params argument.

import pytest

@pytest.fixture(scope="module", params=[config1, config2])
def different_configuration(request):
    return request.param

Then, on your test, you can use each configuration value somehow to change the configuration as you want:

def test_x(self, different_configuration):
    self.different_configurations(different_configuration)
    self.run_and_verify()

Or something like that.

Upvotes: 1

Related Questions