Where should I put tests when packaging python modules?

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)


You should put your test module inside the module it tests according to The Hitchhiker's Guide to Packaging.

Here is their example:

TowelStuff/
    bin/
    CHANGES.txt
    docs/
    LICENSE.txt
    MANIFEST.in
    README.txt
    setup.py
    towelstuff/
        __init__.py
        location.py
        utils.py
        test/
            __init__.py
            test_location.py
            test_utils.py

This way your module will be distributed with its tests and users can use them to verify that it works with their set up.

See http://the-hitchhikers-guide-to-packaging.readthedocs.org/en/latest/creation.html.