Where should I put tests when packaging python modules?

Stephen Paulger picture Stephen Paulger · Mar 17, 2011 · Viewed 8.3k times · Source

I have a module that sits in a namespace. Should tests and data the tests rely on go in the namespace or in the top level where setup.py sites?

./company/__init__.py
./company/namespace/__init__.py
./company/namespace/useful.py
./company/namespace/test_useful.py
./company/namespace/test_data/useful_data.xml
./setup.py

or

./company/__init__.py
./company/namespace/__init__.py
./company/namespace/useful.py
./test_useful.py
./test_data/useful_data.xml
./setup.py

Does the question amount to whether tests should be installed or not?

Answer

guettli picture guettli · Oct 6, 2015

The Sample Project stores the tests outside the module.

The directory structure looks like this:

├── data
│   └── data_file
├── MANIFEST.in
├── README.rst
├── sample
│   ├── __init__.py
│   └── package_data.dat
├── setup.cfg
├── setup.py
└── tests
    ├── __init__.py
    └── test_simple.py

Related: The Packing Guide: https://packaging.python.org/en/latest/

Hint: Don't follow the "The Hitchhiker's Guide to Packaging". It has not been updated since 2010!

(do not confuse both pages. The "The Hitchhiker’s Guide to Python" is a very solid book)