Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any alternative to creating one fixture per variable in a py.test class?

I would like to use some common data in all my py.test class methods, and only in that class, e.g.

n_files = 1000
n_classes = 10
n_file_per_class = int(n_files / n_classes)

I found out that I can use fixtures, e.g.:

class TestDatasplit:

    @pytest.fixture()
    def n_files(self):
        return 1000

    @pytest.fixture()
    def n_classes(self):
        return 10

    @pytest.fixture()
    def n_files_per_class(self, n_files, n_classes):
        return int(n_files / n_classes)

    def test_datasplit_1(self, n_files):
        assert n_files == 1000

    def test_datasplit(self, n_files_per_class):
        assert n_files_per_class == 100

but here I need to create a fixture for all my variables, but that seems quite verbose (I have much more than 3 variables)...

What is the best way to create a bunch of shared variables in a py.test class?

like image 685
jul Avatar asked Oct 29 '25 06:10

jul


1 Answers

Your tests don't seem to be mutating these values, so you can use module-level or class-level constants. Pytest fixtures are there to provide each test with a separate copy of a value, so that tests don't begin to depend on each other (or inadvertently make each other fail) when one or more test mutate the values.

like image 53
das-g Avatar answered Oct 31 '25 10:10

das-g