pytest
pytest
is a program/framework for running Python tests.
Writing Tests
pytest looks for tests in Python files which either begin in test_
or end in _test
, for example:
Inside these files, pytest
will look for either:
- Functions that begin with
test
, or - Functions/methods that begin with
test
that are inside classes that begin withTest
.
For example:
Running Tests
Run All Tests In Module
Run All Tests In Directory
This will also run any tests in sub-directories:
Test Name Matching
You can use -k
to match against test name substrings. The following command will run all tests that contain the stinrg hello
, e.g. test_hello_world
, test_slow_hello()
:
Marks
Marks (or markers) can be applied to test functions using a decorator in the form @pytest.mark.name_of_mark
.
For example, to apply a mark called unit
(note that unit
should defined as a custom mark before you use it like this, see the below Custom Marks section):
You can use the -m
option on the command-line to only run tests with specific marks. The following command will run all tests in the current directory/sub-directory with the mark my_mark
:
A common use-case is to specify tests with test types such as unit
, e2e
, performance
, e.t.c so that you can easily run quick unit tests during development and longer running tests on merge or nightly.
You can also negatively select tests against a mark. The following command will run all tests to marked with unit
:
You can get a list of all the marks you can use from the command-line with:
Custom Marks
Custom marks need to be registered before you can use them. They can be registered in your pytest.ini
file, like so:
As shown above, a description/comment can be added after the :
symbol.
For more information of marks, see https://docs.pytest.org/en/latest/example/markers.html.
conftest.py
conftest.py
files are used to specify directory-specific pytest
features. All conftest.py
files that are at the directory level of the test or closer to the root of the file system will be used when executing pytest
. You can have many conftest.py
files per test project.
Common things to include into conftest.py
files include pytest
hooks and fixtures, as well as loading external plugins specific to the tests in same directory.
Plugins
xdist
xdist allows you to provide the -n
option to distribute the tests to multiple CPUs:
Note however this will prevent all of your print()
statements from working (as well as anything else the prints to stdout
, e.g. log messages). As a workaround, you can redirect stdout
to stderr
:
This can be added to a conftest.py
file so that it applies to all tests in it’s directory and subdirectories. Be warned that all of the output will be interleaved, so it might make the output somewhat useless!
Jenkins
pytest
has the ability to generate junit.xml
files which are used by Jenkins to display the test results.
You can provide the --junitxml <path>
option to pytest
and it will generate the file for you: