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 th
In bash
, try { find -name '*_test.py'; find -name 'test_*.py'; } | xargs pytest
.
For me, this brings total test time down to a fraction of a second.
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.
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.
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.
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 :)
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.
set PYTHONDONTWRITEBYTECODE=1
export PYTHONDONTWRITEBYTECODE=1
subprocess.run
: Add keyword env={'PYTHONDONTWRITEBYTECODE': '1'}
Note that the first two options only remain active for your current terminal session.