如何加速pytest [英] How to speed up pytest
问题描述
有没有办法加快pytest
的重复执行?似乎花费了大量时间收集测试,即使我在命令行上指定要执行哪些文件.我知道这也不是磁盘速度问题,因为在所有 .py 文件中运行 pyflakes 非常快.
Is there some way to speed up the repeated execution of pytest
? It seems to spend a lot of time collecting tests, even if I specify which files to execute on the command line. I know it isn't a disk speed issue either since running pyflakes across all the .py files is very fast.
各种答案代表了 pytest 变慢的不同方式.他们有时有所帮助,而在其他方面则没有.我正在添加一个解释常见速度问题的答案.但是不可能选择The"在这里回答.
The various answers represent different ways pytest can be slow. They helped sometimes, did not in others. I'm adding one more answer that explains a common speed problem. But it's not possible to select "The" answer here.
推荐答案
使用 pytest.ini 或 tox.ini 中的 norecursedirs
选项可以节省大量收集时间,具体取决于您的其他文件在您的工作目录中.当我有一套 300 次测试时,我的收集时间大约减半(0.34 秒对 0.64 秒).
Using the norecursedirs
option in pytest.ini or tox.ini can save a lot of collection time, depending on what other files you have in your working directory. My collection time is roughly halved for a suite of 300 tests when I have that in place (0.34s vs 0.64s).
如果您已经像我一样使用 tox,您只需要添加您的 tox.ini 中的以下内容:
If you're already using tox like I am, you just need to add the following in your tox.ini:
[pytest]
norecursedirs = docs *.egg-info .git appdir .tox
您也可以将其添加到独立的 pytest.ini
文件中.
You can also add it in a free-standing pytest.ini
file.
pytest 文档有关于 pytest 配置文件的更多详细信息.
The pytest documentation has more details on pytest configuration files.
这篇关于如何加速pytest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!