Tests make development easier for both veteran project contributors and newcomers alike. Most projects use the unittest framework for tests so you should familiarize yourself with this framework.
Writing tests can be a great way to get involved with a project. It’s an opportunity to get familiar with the codebase and the code submission and review process. Check the project’s code coverage and write a test for a piece of code missing coverage!
Patches should be accompanied by one or more tests to demonstrate the feature or bugfix works. This makes the review process much easier since it allows the reviewer to run your code with very little effort, and it lets developers know when they break your code.
Having a standard test layout makes it easy to find tests. When adding new tests, follow the following guidelines:
Each module in the application should have a corresponding test module. These modules should be organized in the test package to mirror the package they test. That is, if the package contains the
<package>/server/push.pymodule, the test module should be in a module called
Within each test module, follow the unittest code organization guidelines.
Include documentation blocks for each test case that explain the goal of the test.
Avoid using mock unless absolutely necessary. It’s easy to write tests using mock that only assert that mock works as expected. When testing code that makes HTTP requests, consider using vcrpy.
You may find projects that do not follow this test layout. In those cases, consider re-organizing the tests to follow the layout described here and follow the established conventions for that project until that happens.
Projects should include a way to run the tests with ease locally and the steps to run the tests should be documented. This should be the same way the continuous integration (Jenkins, Zuul CI, etc.) tool runs the tests.
There are many test runners available that can discover unittest based tests. These include:
Projects should choose whichever runner best suits them.
Tox is an easy way to run your project’s tests (using a Python test runner) using multiple Python interpreters. It also allows you to define arbitrary test environments, so it’s an excellent place to run the code style tests and to ensure the project’s documentation builds without errors or warnings.
You can find an example in our CookieCutter template’s
It runs the test suite in various versions of Python, reports the coverage,
runs the pre-commit checks, makes sure we’re not depending on software with
forbidden licenses, and builds the documentation with the
as errors Sphinx flag enabled.
coverage is a good way to collect test coverage statistics. pytest has a pytest-cov plugin that integrates with coverage. Diff-Cover can be used to ensure that all lines edited in a patch have coverage.
It’s possible (and recommended) to have the test suite fail if the coverage is below 100%. See the template’s pyproject.toml file for an example.
An alternative to having 100% coverage is to forbid new changes from bringing
the coverage down. This can be checked by
diff-cover with the following
[tox] envlist = pyXX...,diff-cover,... # If the user is missing an interpreter, don't fail skip_missing_interpreters = True [testenv:diff-cover] deps = diff-cover commands = diff-cover coverage.xml --compare-branch=origin/develop --fail-under=100
You must then make sure that the
pytest command line contains
--cov-report xml to produce the
coverage.xml file that
New projects should enforce 100% test coverage. Existing projects should ensure test coverage does not drop to accept a pull request and should increase the minimum test coverage until it is 100%.
coverage has great exclusion support, so you can exclude individual lines, conditional branches, functions, classes, and whole source files from your coverage report. If you have code that doesn’t make sense to have tests for, you can exclude it from your coverage report. Remember to leave a comment explaining why it’s excluded!
The liccheck checker can verify that every dependency in your project has an acceptable license. The dependencies are checked recursively.
The licenses are validated against a set of acceptable licenses that you
define in a file called
.license_strategy.ini in your project
directory. To avoid having to maintain and sync the same file in every
project, a shared file is available
in the shared repo.
The CookieCutter template contains a shell script that will download the shared file and check the dependencies' licenses against it.
If you want to add a license to the list, feel free to open a Pull Request against the shared repo.
You can automate the license check by running the shell script in Tox.
The bandit checker is designed to find common security issues in Python code.
You can add it to the tests run by Tox by adding the following snippet
[testenv:bandit] deps = bandit commands = bandit -r your_project/ -x your_project/tests/ -ll
Remember to add
bandit to your Tox
Note that Ruff is also capable of running
security checks equivalent to Bandit’s. Make sure you are selecting the
S category in
Want to help? Learn how to contribute to Fedora Docs ›