我有多个测试需要一个昂贵的生成文件. 我希望在每次测试运行时重新生成该文件,但不要超过一次. 更复杂的是,这些测试和文件都依赖于输入参数.
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))
如何确保即使在并行运行这些测试时,该文件也不会跨线程重新生成? 作为背景,我将MakeFiles的测试基础 struct 移植到pytest.
我可以使用基于文件的锁来进行同步,但我相信其他人也遇到过这个问题,他们更愿意使用现有的解决方案.
对于单个线程,使用functools.cache
效果很好.带有scope="module"
的装置根本不起作用,因为参数input
位于函数作用域.