As far as I understood from the documentation about pytest fixture parameterization - it creates copies of the fixture with the given params and thus calls each test which requires this fixture with this different copies.
My needs are a little bit different. Suppose there is a fixture:
@pytest.fixture
def sample_foo():
return Foo(file_path='file/path/test.json')
def test1(sample_foo):
pass
def test2(sample_foo):
pass
The problem is that test test1
and test2
require the same instance of class Foo
but with different values of file_path
So at the moment I do:
def build_foo(path):
return Foo(file_path=path)
def test1():
sample_foo = build_foo('file/path/some.json')
def test2():
sample_foo = build_foo('file/path/another.json')
This looks like a little bit of code duplication. I could create a separate fixtures for each file but that looks even worse. It looks that each test will require it's own unique file, so maybe this can be solved by figuring out the name of the file by looking at the name of the test function requesting the fixture. But that is not guaranteed.