在pytest中是否有常见的添加测试间隔的做法?目前集成测试失败,但如果单独运行测试则正常工作。
@pytest.fixture(autouse=True)
def slow_down_tests():
yield
time.sleep(1)
这个夹具将自动用于所有的测试用例并将执行权交给一个测试用例,以便它可以正常运行,但当测试结束时,执行权会回到这个夹具,然后睡眠将被执行。
import time
def teardown_function(function): # the function parameter is optional
time.sleep(3)
class TestClass:
def teardown(self):
time.sleep(1)
class TestClass:
def teardown_method(self, method):
print(method)
time.sleep(1)
@classmethod
def teardown_class(cls):
print(cls)
time.sleep(2)
你可以在每个测试的拆卸方法中插入time.sleep(1)
,即:
class TestClass:
def setup(self):
pass
def teardown(self):
time.sleep(1) # sleep for 1 second