问题
Is there some way to speed up the repeated execution of py.test
? 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.
回答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 py.test configuration files.
回答2:
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.
回答3:
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 :)
回答4:
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.
回答5:
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.
来源:https://stackoverflow.com/questions/16417546/how-to-speed-up-py-test