Search code examples
pythontestingnamespacesdistribution

Where should I put tests when packaging python modules?


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?


Solution

  • 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.