In my opinion, the correct answer is NO, but you will find many distributions that install tests. Tests should not be installed, but they should be included in the source distribution. In my opinion, in an ideal world, testing installed packages should be the task of the package manager (pip), and the site-packages directory should not be contaminated with testing sources.
I recently researched this topic and gathered information from various sources and found several different ways to structure the directory / package hierarchy of a distribution that contains both library sources and tests. Most of these structures seem to be outdated, and they were invented as attempts to get around incomplete sets of old distribution systems at the time. Unfortunately, many online sources (old blog pages / documentation) still advertise outdated methods, so it’s very easy to find an outdated usage guide / tutorial with online search.
Suppose you have a library called "my_lib" and want to structure your distribution sources. I will show you two popular and seemingly outdated ways to structure your distribution, and the third method, which I consider the most universal. The third method may also be deprecated, but the very best I know is the publication time of this answer .; -)
Method # 1
Distributions that (intentionally or unintentionally) install tests typically use this method.
hierarchies
+- my_lib | +- __init__.py | +- source1.py | +- source2.py | +- tests | +- __init__.py | +- test_1.py | +- test_2.py +- setup.py
Method # 2
Tests are not installed, but they must be included in the source distribution through the MANIFEST.in file.
hierarchies
+- my_lib | +- __init__.py | +- source1.py | +- source2.py +- tests | +- __init__.py | +- test_1.py | +- test_2.py +- setup.py
Method number 3 (I prefer this one).
This is very similar to method # 2 with a slight twist ( src dir).
hierarchies
+- src | +- my_lib | +- __init__.py | +- source1.py | +- source2.py +- tests | +- __init__.py | +- test_1.py | +- test_2.py +- setup.py
setup () call in setup.py
from setuptools import setup, find_packages setup( ... packages=find_packages('src'), package_dir={'': 'src'}, ... )
MANIFEST.in
recursive-include tests *.py
Tests will not be installed, but they will be included in the source distribution through our MANIFEST.in .
For method # 3, you have the src directory, which usually contains only one package, which is the root of your library. Putting the my_lib package in the src directory (a directory, not a package, so you don't need src/__init__.py ) has the following advantages: