How to speed up pytest
Solution 1:
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 pytest configuration files.
Solution 2:
I was having the same problem where I was calling pytest
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 pytest
with the relative path to the tests:
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:
[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.
Solution 3:
With xdist you can parallelize pytest runs. It allows even to ship tests to remote machines. Depends on your setup it can speedup quite a bit :)
Solution 4:
For me, adding PYTHONDONTWRITEBYTECODE=1
to my environment variables achieved a massive speedup! Note that I am using network drives which might be a factor.
- Windows Batch:
set PYTHONDONTWRITEBYTECODE=1
- Unix:
export PYTHONDONTWRITEBYTECODE=1
-
subprocess.run
: Add keywordenv={'PYTHONDONTWRITEBYTECODE': '1'}
- PyCharm already set this variable automatically for me.
Note that the first two options only remain active for your current terminal session.