Pytest running very slow for project

For me the issue was with a particular conftest.py taking a very long time to run (it was setting up some huge fixtures for a different test that I didn't need here).

This was tricky to diagnose, since the delay still occured with --collect-only flag and custom rootdir set to the test directory only (the conftest was in a different directory far away but was still being detected somehow).

The solution for me was to run pytest --noconftest.


More or less restating my other answer:

When you invoke pytest, it will scan every subdirectory in project root, looking for tests. This may slow down the test collection; it may be wise to exclude unrelated directories from being scanned. pytest offers two configuration options for that:

  1. norecursedirs - holds directories that will be excluded from scanning. Use this option when you are looking for the pattern "include all, exclude selected". By default, norecursedirs is set to '.*', 'build', 'dist', 'CVS', '_darcs', '{arch}', '*.egg', so beware that when you override this option, the defaults are gone and you have to add them back.
  2. testpaths - holds directories that should only be considered for the scan, so this is basically the opposite to what norecursedirs is doing. Use this option when looking for the pattern "exclude all, include selected". This option also adds some minor or more significant speedup to the test discovery, depending on what you keep in the project root - most of the sudbirectories won't be traversed at all and the tests run starts sooner.

Usage: either place the options in the pytest.ini/setup.cfg/tox.ini:

[tool:pytest]
testpaths = tests othertests doc

or pass them via --override-ini from command line.

pytest -o "testpaths=tests othertests doc" ...