Python Test standard library doctest

Source: Internet
Author: User

Introduction:

Doctest is a standard library for testing in Python. This library is used to simulate console dialogs to detect correct values.

As the name implies, this module is looking for a part of the program inside a python interactive interpreter, and then running this section and comparing the results provided to determine whether the actual running results are consistent with the results you want.

Basic use: 1. Pass docstrings Test

Let's start by creating a example.py with the following code

1 " "2 Docstrings can put the position 134 >>> sum (1, 3)5 46 7 " "8 9 Ten defsum (A, b): One     " " A Docstrings can put the position 2 - >>> sum (3, 5) - 8 the      - >>> sum (' A ', ' C ') - ' AC ' -     " " +     returnA +b -      + if __name__=="__main__": A     Importdoctest atDoctest.testmod ()

Then we execute the command Python example.py-v in the console, and the result of the output test is as follows

$ python example.py-vtrying:    sum (1, 3) expecting:    4okTrying:    sum (3, 5) Expecting:    8okTrying:    sum (' A ', ' C ') Expecting:    ' ac ' ok2 items passed all tests:   1 Tests in __main__   2 tests in __main__.sum3 tests in 2 items.3 PA Ssed and 0 failed. Test passed.

Note that in the case where the sample is all passed, without failed, "Python example.py" is not outputting anything, it needs to add parameter-V to print all the test examples. We can replace the 5th line of examp.py with the wrong result 99, in the execution of "Python example.py", then we will show a sample that has not passed, as follows.

$ python example.py**********************************************************************"  example.py"in__main__Failed Example:    sum (1, 3) expected :    Got:    4**********************************************************************1 items had failures:    1 of in   __main__***test failed*** 1 failures.

It is also important to note that the document comments inside the

>>> sum (1, 3)

After ">>>" Remember to follow a space

Let's see it again.

if __name__ " __main__ " :     Import doctest    doctest.testmod ()

If you do not want to execute Testmod () in __main__, you can also do it without writing python-m doctest-v example.py.

2. Pass the test file tests

We create a new text file Example.txt, which writes

>>> from example Import sum>>> sum (2, 3) 5

Then replace the Testmod () function in example.py with the Testfile ()

Import doctestdoctest.testfile ("example.txt")

And then, like 1, run Python example.py-v

Similarly, if you do not want to run testfile () in example.py, you can also use the following command

Python-m doctest-v Example.txt

Resources:

Official Python document: https://docs.python.org/3/library/doctest.html

"Fluent Python"

\python36\lib\test\test_doctest.py

Python Test standard library doctest

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.