pytest中的测试用例执行顺序 [英] Test case execution order in pytest

查看:52
本文介绍了pytest中的测试用例执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 pytest.我在一个目录中有两个文件.在其中一个文件中,有一个长时间运行的测试用例会生成一些输出.在另一个文件中有一个读取该输出的测试用例.如何确保两个测试用例的正确执行顺序?除了将测试用例以正确的顺序放在同一个文件中之外,还有其他选择吗?

I am using pytest. I have two files in a directory. In one of the files there is a long running test case that generates some output. In the other file there is a test case that reads that output. How can I ensure the proper execution order of the two test cases? Is there any alternative other than puting the test cases in the same file in the proper order?

推荐答案

通常,您可以使用其 明确指定的钩子.

In general you can configure the behavior of basically any part of pytest using its well-specified hooks.

在您的情况下,您需要pytest_collection_modifyitems"挂钩,可让您重新排序收集到的测试.

In your case, you want the "pytest_collection_modifyitems" hook, which lets you re-order collected tests in place.

也就是说,订购测试似乎应该更容易——毕竟这是 Python!所以我写了一个用于排序测试的插件:pytest-ordering".查看 docs 或从 pypi.现在我建议使用@pytest.mark.first 和@pytest.mark.second,或@pytest.mark.order# 标记之一,但我对更有用的API 有一些想法.欢迎提出建议:)

That said, it does seem like ordering your tests should be easier -- this is Python after all! So I wrote a plugin for ordering tests: "pytest-ordering". Check out the docs or install it from pypi. Right now I recommend using @pytest.mark.first and @pytest.mark.second, or one of the @pytest.mark.order# markers, but I have some ideas about more useful APIs. Suggestions welcome :)

pytest-ordering 目前似乎已被放弃,您还可以查看 pytest-order(作者原创项目的一个分支).

pytest-ordering seems abandoned at the moment, you can also check out pytest-order (a fork of the original project by the author).

这篇关于pytest中的测试用例执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆