pytest 运行 新测试(几乎)优先

pytest run new tests (nearly) first

我正在使用 pytest。我喜欢我调用 pytest 的方式(首先重试失败的测试,冗长,抓取并显示串行输出,在第一次失败时停止):

 pytest --failed-first -v -s -x

不过我还想要一件事:

我希望 pytest 运行 新测试(即之前从未测试过的测试)紧接着 --首先失败个。这样,在处理执行时间很长的测试时,我会尽快获得最相关的信息。

有什么办法吗?

这可能不是您要问的直接问题,但是,我的理解是,在开发过程中创建新测试时,测试执行顺序对您很重要。

由于您已经在使用这些 "new" 测试,因此 pytest-ordering 插件可能是一个不错的选择。它允许您通过使用 @pytest.mark.first@pytest.mark.second 等装饰器装饰您的测试来影响执行顺序。


pytest-ordering 可以通过使用 pytest_collection_modifyitems hook. There is also pytest-random-order plugin which also uses the same hook 更改执行顺序到 control/change 顺序。

您还可以根据自己的具体需要定义和调整自己的挂钩。例如,这里使用另一个钩子来打乱测试:

对于现在来到这里的任何人,pytest added a --new-first option to run new tests before all other tests. It can be combined with --failed-first to run new and failed tests. For test-driven development, I've found it helpful to use these options with pytest-watch, which I described in my blog