This chapter covers
- Running unit tests with pytest
- Creating test coverage reports with pytest-cov
- Reducing duplicated test code with parametrization
- Automating packaging for testing using tox
- Creating a test matrix
Tests are an important aspect of any project you plan to maintain. They can ensure that new functionality behaves as you expect, and that existing functionality hasn’t regressed. Tests are the guardrails for refactoring code—a common activity as projects mature.
With all this value that tests provide, you might think all open-source packages would be thoroughly tested. But many projects pass on things like code coverage or testing for multiple target platforms because of the maintenance burden they present. Some maintainers even create maintenance burden without realizing it, due to the way they design and run their test suite. In this chapter you’ll learn some beneficial aspects of testing and how to introduce them to your package’s test suite, with an eye toward automation and scalability.
If you’re still new to unit testing concepts, you can learn all about them in The Art of Unit Testing, Third Edition by Roy Osherove (https://www.manning.com/books/the-art-of-unit-testing-third-edition).
Important
You can use the code companion (https://github.com/daneah/publishing-python-packages) to check your work for the exercises in this chapter.