【问题标题】:Pytest schedule intervals between groups of testsPytest 安排测试组之间的时间间隔
【发布时间】:2020-12-14 01:03:11
【问题描述】:

有没有办法告诉 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/,它似乎提供了接近我正在寻找的行为。我只需要一种在某些测试组之间等待的方法。

【问题讨论】:

    标签: python pytest


    【解决方案1】:

    您可以将所有第一部分测试合并到一个类中,将所有第二部分测试合并到另一个类中,并使用类范围固定装置进行延迟,如下所示:

    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
    

    【讨论】:

    • 谢谢,这正是我所需要的。我正在研究的解决方案只是使用 pytest-ordering,然后插入测试,这些测试只会等待一定的时间然后通过,但这似乎是一种更简洁的做事方式。
    猜你喜欢
    • 2018-05-25
    • 2018-02-07
    • 2021-02-06
    • 2021-08-21
    • 2019-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多