Python/Setuptools/Testing

From Jonathan Gardner's Tech Wiki
Jump to: navigation, search

Add Tests

To add tests to your project, follow these simple steps:

1. Create a test package or module. I name mine "tests". (This could be tests/__init__.py or just tests.py) This package/module belongs in the root of your package.

2. Add the following line to setup.py's setup invokation:

test_suite='tests'

This tells setuptools that the default test to run is simply 'tests'.

3. Modify the tests module to include several objects derived from unittest.TestCase. These will each be run as part of the test. If you need to, move the tests to separate modules and have tests import them. This way they can be run separately or all together.

Running Tests

To run all the tests (the test_suite attribute in setup), simply do:

python setup.py test

You can override the test suite to run with the -s/--test-suite option. Simple put in the name of the module with the tests.

python setup.py test -s tests.foo

See Also