How to speed up py.test

心不动则不痛 提交于 2019-11-28 08:02:00

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).

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

You can also add it in a free-standing pytest.ini file.

The pytest documentation has more details on py.test configuration files.

I was having the same problem where I was calling py.test at the root of my project and my tests were three subdirectories down. The collection was taking 6-7 seconds before 0.4 seconds of actual test execution.

My solution initially was to call py.test with the relative path to the tests:

py.test 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:

[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 py.test as I was before.

With xdist you can parallelize py.test runs. It allows even to ship tests to remote machines. Depends on your setup it can speedup quite a bit :)

rocky

In the special case where you are running under cygwin's python, its unix-style file handling is slow. See pytest.py test very slow startup in cygwin for how to speed things up in that special situation.

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. 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!