1, the integrated module doctest
doctest is python
built in a standard library, it can find the code similar to the interactive session in the form of comments, and check whether they are correct;
(1) by default, pytest
automatically collect the names of all matching test*.txt
files rule, and calls doctest
execute them; doctest
the default file encoding is UTF-8 , you can pytest.ini
use doctest_encoding
specify a new encoding options;
(2) strings by writing documentation
--doctest-modules
Finds all matching names *.py
of files, documents collected in the form of an interactive session string of similar comments, and to each document string as a use case is executed, so the implementation of the above two tests, one of which is a test document;
If you want pytest --doctest-modules
the right to collect relevant comments must meet the following conditions:
- File names conform to
*.py
the rules, but do not need to meettest_*.py
or*_test.py
rules; - Docstring comments must be similar to the
python
interactive session in the form of comments;
Add --doctest-modules can be summarized parameter entry when configured --doctest-modules in each execution without pytest.ini
(3) specify additional item
- doctest built: https://docs.python.org/3/library/doctest.html#option-flags
- pytest comes
ALLOW_BYTES
: When the output character string excludingb
prefixedALLOW_UNICODE
: Similarly, at the output, excluding the stringu
prefix;NUMBER
: In order to avoid the situation that caused the failure:
2, continue on failure
By default, for a given document test, pytest
to withdraw in the face of the first point of failure; however, you can --doctest-continue-on-failure
command line options, allowed to continue;
3, the output format designated
Documents test fails, you can change the test output format of the following ways:
- pytest --doctest-modules --doctest-report none
- pytest --doctest-modules --doctest-report udiff
- pytest --doctest-modules --doctest-report cdiff
- pytest --doctest-modules --doctest-report ndiff
- pytest --doctest-modules --doctest-report only_first_failure
4, the document used in the test fixture
By getfixture
using strings in the document allows fixture
:
5, document test namespace
doctest_namespace fixture
Operation may be used to doctest
inject some information namespace tested, which is a standard dictionary object;
6, skipping the test document
By pytest.skip
skipping the test documentation;