Eight, integrated document test

1, the integrated module doctest

doctest is pythonbuilt 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, pytestautomatically collect the names of all matching test*.txtfiles rule, and calls doctestexecute them; doctestthe default file encoding is UTF-8 , you can pytest.iniuse doctest_encodingspecify a new encoding options;

(2) strings by writing documentation

 

 

--doctest-modulesFinds all matching names *.pyof 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-modulesthe right to collect relevant comments must meet the following conditions:

  • File names conform to *.pythe rules, but do not need to meet test_*.pyor *_test.pyrules;
  • Docstring comments must be similar to the pythoninteractive 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 excluding bprefixed
    • ALLOW_UNICODE: Similarly, at the output, excluding the string uprefix;
    • NUMBER: In order to avoid the situation that caused the failure:  

2, continue on failure

By default, for a given document test, pytestto withdraw in the face of the first point of failure; however, you can --doctest-continue-on-failurecommand 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 getfixtureusing strings in the document allows fixture:

5, document test namespace

doctest_namespace fixtureOperation may be used to doctestinject some information namespace tested, which is a standard dictionary object;

6, skipping the test document

By pytest.skipskipping the test documentation;

 

Guess you like

Origin www.cnblogs.com/Tester-Chenmo/p/12553511.html