有没有办法告诉 pytest 运行一组特定的测试,然后等待一段已知的时间,然后运行另一组测试?例如,如果我有以下要求的测试:每个测试有 3 个部分(执行 3 个方法)在运行第 1 部分后经过特定的已知时间量之前,不得为每个测试运行第 2 部分。在运行第 2 部分后经过特定的已知时间量之前,不得为每个测试运行第 3 部分。如果我将每个测试的第 1、2 和 3 部分拼接在一起,并且只使用 time.sleep(),那么执行所有测试将花费太长时间。相反,我想背靠背运行所有第 1 部分,然后等待一段已知时间,然后背靠背运行所有第 2 部分,然后等待一段已知时间,然后运行所有第 3 部分。看来这应该可以使用标记https://docs.pytest.org/en/stable/example/markers.html来实现,并可能实现挂钩https://docs.pytest.org/en/latest/reference。 html#hooks根据使用的标记实现某些行为,尽管我对 pytest 钩子不是很熟悉。我还遇到了 pytest-ordering https://pytest-ordering.readthedocs.io/en/develop/,它似乎提供了接近我正在寻找的行为。我只需要一种在某些测试组之间等待的方法。
1 回答

森林海
TA贡献2011条经验 获得超2个赞
您可以将所有第一部分测试组合在一个类中,将所有第二部分测试组合在另一个类中,并使用类范围固定装置来延迟,如下所示:
import pytest
import time
@pytest.fixture(scope='class')
def delay():
time.sleep(5)
class TestPart1:
def test_one_part_1(self):
assert 1 == 1
def test_two_part_1(self):
assert 2 == 2
@pytest.mark.usefixtures("delay")
class TestPart2:
def test_one_part_2(self):
assert 1 == 1
def test_two_part_2(self):
assert 2 == 2
添加回答
举报
0/150
提交
取消