我有一组用于不同 UI 测试的测试文件(.py 文件)。 我想使用 pytest 按特定顺序运行这些测试文件。我使用了下面的命令
python -m pytest -vv -s --capture=tee-sys --html=report.html --self-contained-html ./Tests/test_transTypes.py ./Tests/test_agentBank.py ./Tests/test_bankacct.py
pytest 执行是由 AWS Batch 作业触发的。 当测试执行发生时,它不会按照上述命令中指定的顺序执行测试文件。 相反,它首先运行 test_agentBank.py,然后运行 test_bankacct.py,然后运行 test_transTypes.py 每个 python 文件都包含一堆测试函数。
我还尝试装饰测试函数类,例如第一个python文件(test_transTypes.py)中的@pytest.mark.run(order=1),第二个python中的@pytest.mark.run(order=2)文件(test_agentBank.py)等。 这似乎按顺序运行测试,但最后我收到警告
PytestUnknownMarkWarning: Unknown pytest.mark.run - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs
.pytest.org/en/stable/how-to/mark.html
@pytest.mark.run(order=1)
在 pytest 中按特定顺序运行测试的正确方法是什么? 我的每个“test_”python 文件都是我需要使用 pytest 运行的文件。
非常感谢任何帮助。
最佳答案
要指定 pytest 中运行测试的顺序,可以使用 pytest-order插入。该插件允许您通过提供 order
标记来自定义测试运行的顺序,该标记具有定义测试何时应相对彼此运行的属性。您可以使用绝对属性(例如,第一个、倒数第二个)或相对属性(例如,在另一个测试之前运行此测试)来指定测试的顺序。这是一个例子:
import pytest
@pytest.mark.order(2)
def test_foo():
assert True
@pytest.mark.order(1)
def test_bar():
assert True
关于python - 测试套件的 Pytest 排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75598463/