Search code examples
pytestfixtures

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?


Solution

  • I agree with what @das-g said, but if you wanted to use fixtures, then you could have a fixture which returns a object based on a custom class, or e.g. a namedtuple.