有没有办法告诉 pytest 运行一组特定的测试,然后等待已知的时间,然后运行另一组测试?例如,如果我有满足以下要求的测试:
- 每个测试有 3 个部分(3 个执行方法)
- 在运行第 1 部分之后经过特定的已知时间后,不得为每个测试运行第 2 部分。
- 在运行第 2 部分之后经过特定的已知时间后,不得为每个测试运行第 3 部分。
- 如果我将每个测试的第 1、2 和 3 部分拼接在一起并仅使用 time.sleep(),则执行所有测试将花费太长时间。
- 相反,我想连续运行所有第 1 部分,然后等待已知的时间,然后连续运行所有第 2 部分,然后等待已知的时间,然后运行所有部分3秒。
看来这应该可以使用标记来实现 https://docs.pytest.org/en/stable/example/markers.html并可能实现钩子(Hook) https://docs.pytest.org/en/latest/reference.html#hooks尽管我对 pytest 钩子(Hook)不太熟悉,但根据所使用的标记实现某些行为。
我还遇到了 pytest-ordering https://pytest-ordering.readthedocs.io/en/develop/这似乎提供了接近我正在寻找的行为。我只需要一种在某些测试组之间等待的方法。
最佳答案
您可以将所有第一部分测试合并到一个类中,将所有第二部分测试合并到另一个类中,并使用类范围固定装置来延迟,如下所示:
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
关于python - Pytest 安排测试组之间的间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63577134/