我使用以下方法通过py.test模拟常量值以进行测试:
@patch('ConstantsModule.ConstantsClass.DELAY_TIME', 10)
def test_PowerUp():
...
thing = Thing.Thing()
assert thing.a == 1
Run Code Online (Sandbox Code Playgroud)
这模拟了测试和Thing中使用的DELAY_TIME,这正是我所期望的。
我想对这个文件中的所有测试执行此操作,所以我尝试了
@patch('ConstantsModule.ConstantsClass.DELAY_TIME', 10)
@pytest.fixture(autouse=True)
def NoDelay():
pass
Run Code Online (Sandbox Code Playgroud)
但这似乎没有相同的效果。
这是一个类似的问题:pytest夹具中的pytest-mock模拟程序,但该模拟似乎是通过非装饰器方式完成的。