在pytest中,在测试之间添加间隔是一种常见的做法吗?目前,集成测试失败,但如果单独运行测试,则可以正常工作。
发布于 2018-02-04 20:11:27
如果您想要在模块的每个函数的模块中进行拆卸
import time
def teardown_function(function): # the function parameter is optional
time.sleep(3)
如果您希望在类的每个方法的类中都有一个teardown,那么您有两个选择。
类TestClass: def teardown(自身):time.sleep(1)
如果需要访问
类方法:定义方法(self,time.sleep(1) ):print(TestClass)teardown_method
如果您想要在类之后调用一次的teardown
@classmethod
def teardown_class(cls):
print(cls)
time.sleep(2)
对于设置,所有这些方法都以相同的方式工作。你可以看到the documentation。在更复杂的实现中使用fixtures。
发布于 2021-04-07 02:22:50
您可以在pytest中使用autouse fixtures在测试用例之间自动休眠:
@pytest.fixture(autouse=True)
def slow_down_tests():
yield
time.sleep(1)
此fixture将自动用于所有测试用例,并将生成测试用例的执行,因此它可以正常运行,但当测试完成时,执行将返回到此fixture并运行睡眠。
发布于 2018-01-02 21:10:03
您可以在每次测试的teardown方法中插入time.sleep(1)
,即:
class TestClass:
def setup(self):
pass
def teardown(self):
time.sleep(1) # sleep for 1 second
https://stackoverflow.com/questions/47770037
复制相似问题