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.
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.
In pytest. ini, set fastest_commit to the name of a Git commit to compare your current work against. (You can also set or override it on the command line with --fastest-commit). This is required if you want to skip tests, which is the main reason for using this plugin.
In summary, if you have slow compiling speeds with pytest, try specifying the directory or file your test(s) are in. Or use norecursedirs to skip directories that don't have any tests, like src or . git . in my case it takes 20 seconds to load, even by specifying a test file with 2 unit tests inside.
For cleanup (usually not needed), a --cache-clear option allows to remove all cross-session cache contents ahead of a test run. Other plugins may access the config. cache object to set/get json encodable values between pytest invocations.
Advantages Of pytest Tests are run parallel. Specific tests and subsets of tests can be run from the program. It is easy to start with as it has a very easy syntax.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With