The Pytest framework is simple to use, rich in plugins, and powerful, and it is widely used for Python automated testing. This article introduces some basic concepts and usage of Pytest.
1. How It Runs
Step one, Pytest reads its configuration from the command line or a file.
Step two, it looks for and imports conftest.py files in the specified directory.
Step three, it finds test files that match the criteria, usually py files starting with test_.
Step four, it executes session or module level fixtures.
Step four, it finds and runs the test cases defined in classes and functions.
2. The pytest.ini and conftest.py Files
First, let’s look at how test files are organized:
| |
When running Pytest, you can pass runtime parameters on the command line, and you can also use a configuration file. The order in which Pytest searches for configuration files is:
| |
Usually you place a pytest.ini file in the project and configure the relevant parameters there.
pytest.ini:
| |
tests/conftest.py, the global dependency configuration
| |
When running the tests, execute the command:
| |
3. pytest.fixture
A fixture is a concept introduced by Pytest and one of the powerful features it provides. Let’s look at how it is used:
3.1 Using It Directly as a Constant
| |
3.2 Running It as a Setup Function
A fixture lets you run some preparatory actions before a test case executes.
| |
With autouse and scope, you can handle the preparation for many test cases.
3.3 Scope
The scope is declared with the scope parameter. There are four options, and the default is function:
- function, function level, executed once for every test function
- class, class level, executed once per test class, available to all its methods
- module, module level, executed once per module, available to all functions and methods in the module
- session, session level, executed only once per test run, available to all functions and methods that are found
| |
4. pytest.mark
pytest.mark is used to mark test cases, giving you finer-grained control over them.
A marker does not need to be defined beforehand to be used. Here is an example:
| |
Some built-in markers:
- skip, skips the decorated test case
- skipif, takes a condition and skips the test when it is met
- xfail, treats a failure as a pass and a success as a failure
- parametrize, supplies data to a test case in batches. If the parameter name of parametrize is the same as a fixture name, it overrides the fixture.
| |
5. Common Plugins
5.1 pytest-cov
pytest-cov is a plugin that automatically measures test coverage. Example usage:
| |
5.2 pytest-mock
Mocking is meant to shield a test from some of its dependencies. A dependency should have its own test cases; each test only needs to care about whether its own functionality works. Example usage:
| |
5.3 pytest-html
pytest-html is a plugin that automatically generates test reports in HTML format. Example usage:
| |
5.4 pytest-django
pytest-django adds Pytest support to Django applications and projects. Specifically, pytest-django brings the ability to test Django projects with pytest fixtures, and it runs faster than the standard Django test suite.
