Search code examples
pythontestingbuildsetuptoolspython-wheel

Python: run tests from wheel or sdist


For a package I've authored, I've done python setup.py sdist bdist_wheel, which generates some package artifacts in the dist/ directory. Now I'd like to run the package's unit tests in those artifacts. What's a good way to do it?

To be clear: an alternative would be to run the tests directly from the local source files, but I want to avoid that to make sure I'm testing the exact pre-built artifact users would be installing (as suggested here).

I'm using Python 3, and I'm on a Linux or Mac OS environment. My context is a build server that builds, tests, and then publishes artifacts (to a private PyPI-like repo) as commits are made to a Git repository.

If there's some other approach I should be using instead, I'm all ears.


Solution

  • What you can do is:

    • Create a virtual environment
    • Install your package
    • Run the tests against your installed library using tools like pytest, you can read more about pytest good practices here: http://pytest.org/dev/goodpractises.html

    As pointed in the pytest docs take a look to tox as well for your CI server: http://pytest.org/dev/goodpractises.html#use-tox-and-continuous-integration-servers

    This is a related question regarding how to test using the installed package: Force py.test to use installed version of module