"does it make sense to install my python unit tests in site-packages?" Code Answer


after researching this issue, and until someone more experienced has a minute to weigh in to the contrary, my understanding is that the simple answer is: "no, unit tests should not be installed, only included in the source distribution".

in the handful of cases i found where tests were installed, all turned out to be accidental and it's easier than one might think to make the mistake without noticing it.

here's how it happens:

  1. the packages=find_packages() parameter is used in setup.py so packages can be found without having to list them out explicitly.
  2. the test folder is made into a package (by adding __init__.py) so tests can reference the modules they test using relative naming (like from .. import pkg.mod).
  3. setuptools installs test as a separate package, alongside the other(s) in the project. note this means you can execute import test in the python interpreter and it works, almost certainly not what you intended, especially since a lot of other folks use that name for their test directory :)

the fix is to use the setting: packages=find_packages(exclude=['test']) to prevent your test directory from being installed.

By yukitos on July 13 2022

Answers related to “does it make sense to install my python unit tests in site-packages?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.