简体   繁体   English

如何加速pytest

[英]How to speed up pytest

Is there some way to speed up the repeated execution of pytest ?有什么方法可以加快pytest的重复执行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.我知道这也不是磁盘速度问题,因为在所有 .py 文件中运行 pyflakes 非常快。


The various answers represent different ways pytest can be slow.各种答案代表了 pytest 可能变慢的不同方式。 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.但是这里不可能选择“The”答案。

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.使用 pytest.ini 或 tox.ini 中的norecursedirs选项可以节省大量收集时间,具体取决于您的工作目录中还有哪些其他文件。 My collection time is roughly halved for a suite of 300 tests when I have that in place (0.34s vs 0.64s).当我进行了 300 次测试时,我的收集时间大约减半(0.34 秒对 0.64 秒)。

If you're already using tox like I am, you just need to add the following in your tox.ini:如果你已经像我一样使用tox ,你只需要在你的 tox.ini 中添加以下内容:

[pytest]
norecursedirs = docs *.egg-info .git appdir .tox

You can also add it in a free-standing pytest.ini file.您还可以将其添加到独立的pytest.ini文件中。

The pytest documentation has more details on pytest configuration files . pytest 文档有关于pytest 配置文件的更多细节。

I was having the same problem where I was calling pytest at the root of my project and my tests were three subdirectories down.我遇到了同样的问题,我在项目的根目录下调用pytest并且我的测试在三个子目录下。 The collection was taking 6-7 seconds before 0.4 seconds of actual test execution.在实际测试执行 0.4 秒之前,收集需要 6-7 秒。

My solution initially was to call pytest with the relative path to the tests:我的解决方案最初是使用测试的相对路径调用pytest

pytest src/www/tests/

If doing that speeds up your collection also, you can add the relative path to the tests to the end of the addopts setting in your pytest.ini - eg:如果这样做也可以加快您的收集速度,您可以将测试的相对路径添加到addopts设置的pytest.ini - 例如:

[pytest]
addopts = --doctest-glob='test_*.md' -x src/www/tests/

This dropped the collection + execution time down to about a second and I could still just call pytest as I was before.这将收集 + 执行时间降低到大约一秒钟,我仍然可以像以前一样调用pytest

With xdist you can parallelize pytest runs.使用xdist,您可以并行化 pytest 运行。 It allows even to ship tests to remote machines.它甚至允许将测试发送到远程机器。 Depends on your setup it can speedup quite a bit :)取决于您的设置,它可以加快相当多的速度:)

In the special case where you are running under cygwin's python, its unix-style file handling is slow.在您在 cygwin 的 python 下运行的特殊情况下,其 unix 样式的文件处理速度很慢。 See pytest.py test very slow startup in cygwin for how to speed things up in that special situation.有关如何在这种特殊情况下加快速度的信息,请参阅pytest.py test very slow startup in cygwin

In bash , try { find -name '*_test.py'; find -name 'test_*.py'; } | xargs pytestbash ,尝试{ find -name '*_test.py'; find -name 'test_*.py'; } | xargs pytest { find -name '*_test.py'; find -name 'test_*.py'; } | xargs pytest { find -name '*_test.py'; find -name 'test_*.py'; } | xargs pytest . { find -name '*_test.py'; find -name 'test_*.py'; } | xargs pytest

For me, this brings total test time down to a fraction of a second.对我来说,这将总测试时间缩短到几分之一秒。

If you have some antivirus software running, try turning it off.如果您正在运行某些防病毒软件,请尝试将其关闭。 I had this exact same problem.我有这个完全相同的问题。 Collecting tests ran incredibly slow.收集测试运行得非常慢。 It turned out to be my antivirus software (Avast) that was causing the problem.结果是我的防病毒软件 (Avast) 导致了问题。 When I disabled the antivirus software, test collection ran about five times faster.当我禁用防病毒软件时,测试收集的运行速度大约快了五倍。 I tested it several times, turning the antivirus on and off, so I have no doubt that was the cause in my case.我测试了几次,打开和关闭防病毒软件,所以我毫不怀疑这是我的原因。

Edit: To be clear, I don't think antivirus should be turned off and left off.编辑:明确地说,我认为不应该关闭和停止防病毒软件。 I just recommend turning it off temporarily to see if it is the source of the slow down.我只是建议暂时关闭它,看看它是否是减速的根源。 In my case, it was, so I looked for other antivirus solutions that didn't have the same issue.就我而言,确实如此,所以我寻找了其他没有相同问题的防病毒解决方案。

For me, adding PYTHONDONTWRITEBYTECODE=1 to my environment variables achieved a massive speedup!对我来说,将PYTHONDONTWRITEBYTECODE=1添加到我的环境变量中实现了巨大的加速! Note that I am using network drives which might be a factor.请注意,我正在使用网络驱动器,这可能是一个因素。

  • Windows Batch: set PYTHONDONTWRITEBYTECODE=1 Windows 批处理: set PYTHONDONTWRITEBYTECODE=1
  • Unix: export PYTHONDONTWRITEBYTECODE=1 Unix: export PYTHONDONTWRITEBYTECODE=1
  • subprocess.run : Add keyword env={'PYTHONDONTWRITEBYTECODE': '1'} subprocess.run :添加关键字env={'PYTHONDONTWRITEBYTECODE': '1'}
  • PyCharm already set this variable automatically for me. PyCharm 已经为我自动设置了这个变量。

Note that the first two options only remain active for your current terminal session.请注意,前两个选项仅对您当前的终端会话保持活动状态。

Pytest imports all modules in the testpaths directories to look for tests. Pytest 导入testpaths目录中的所有模块以查找测试。 The import itself can be slow.导入本身可能很慢。 This is the same startup time you'd experience if you ran those tests directly, however, since it imports all of the files it will be a lot longer.如果您直接运行这些测试,这与您所经历的启动时间相同,但是,由于它会导入所有文件,因此需要更长的时间。 It's kind of a worst-case scenario.这是一种最坏的情况。

This doesn't add time to the whole test run though, as it would need to import those files anyway to execute the tests.但这不会为整个测试运行增加时间,因为无论如何它都需要导入这些文件来执行测试。

If you narrow down the search on the command line, to specific files or directories, it will only import those ones.如果您将命令行上的搜索范围缩小到特定文件或目录,它只会导入这些文件或目录。 This can be a significant speedup while running specific tests.在运行特定测试时,这可能是一个显着的加速。

Speeding up those imports involves modifying those modules.加速这些导入涉及修改这些模块。 The size of the module, and the transitive imports, slow down the startup.模块的大小和可传递的导入会减慢启动速度。 Additionally look for any code that is executed -- code outside of a function.另外查找任何执行的代码——函数之外的代码。 That also needs to be executed during the test collection phase.这也需要在测试收集阶段执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM