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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With