当 pytest 运行目录中的所有测试时,它如何决定最后运行哪个测试?

use*_*164 6 python pytest

我使用 pytest 调用目录下的所有测试。我怎样才能最后运行一个特定的测试用例?

python -m pytest ../testdir  
../testdir/test_1.py.... test_n.py
Run Code Online (Sandbox Code Playgroud)

hoe*_*ing 5

您可以通过实现自己的挂钩轻松更改默认测试执行顺序pytest_collection_modifyitemsconftest.py创建一个包含以下内容的文件:

def pytest_collection_modifyitems(items):
    test_name = 'test_1'
    test_index = next((i for i, item in enumerate(items) if item.name == test_name), -1)
    test_item = items.pop(test_index)
    items.append(test_item)
Run Code Online (Sandbox Code Playgroud)

在此示例中,如果收集了名为的测试函数test_1,则它将移至项目列表的末尾。替换test_1为您自己的函数名称,甚至可以通过命令行参数进行配置