I have multiple tests that need require an expensive-to-generate file.I'd like the file to be re-generated on every test run, but no more than once.To complicate the matter, both these tests as well as the file depend on an input parameter.
def expensive(param) -> Path: # Generate file and return its path.@mark.parametrize('input', TEST_DATA)class TestClass: def test_one(self, input) -> None: check_expensive1(expensive(input)) def test_two(self, input) -> None: check_expensive2(expensive(input))
How can make sure that this file is not regenerated across threads even when running these tests in parallel?For context, I'm porting test infrastructure that Makefiles to pytest.
I'd be OK, with using file-based locks to synchronize, but I'm sure someone else has had this problem and would rather use an existing solution.
Using functools.cache
works great for a single thread. Fixtures with scope="module"
doesn't work at all, because the parameter input
is at function scope.