Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to speed up pytest

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.

like image 397
edA-qa mort-ora-y Avatar asked May 07 '13 11:05

edA-qa mort-ora-y


People also ask

How can I make my pytest faster?

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.

Why pytest is taking too long?

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.

Can I delete pytest cache?

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.

Is pytest easy?

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.


2 Answers

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.

like image 176
scanny Avatar answered Sep 23 '22 13:09

scanny


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.

like image 41
thisismyrobot Avatar answered Sep 24 '22 13:09

thisismyrobot