Excerpt from: Https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/ 0014319170285543a4d04751f8846908770660de849f285000
When we write a comment, write a comment like this:
def ABS (N): " " Function to get absolute value of number. Example: >>> ABS (1) 1 >>> abs ( -1) 1 >>> abs (0) 0 ' returnifelse (-N)
It is undoubtedly more explicit to tell the caller of the function the expected input and output of the function.
Also, Python 's built-in "Document Test" (doctest) module can extract the code directly from the comments and perform the test.
Doctest strictly follows the input and output of the Python interactive command line to determine whether the test results are correct. Only when you test an exception, you can use ...
a large section of the output that is annoying.
Let's use Doctest to test the last class we wrote Dict
:
#mydict2.pyclassDict (Dict):" "Simple Dict-also support access as x.y style. >>> D1 = Dict () >>> d1[' x '] = + >>> d1.x >>> d1.y = >>& Gt d1[' y '] >>> d2 = Dict (A=1, b=2, c= ' 3 ') >>> d2.c ' 3 ' >>> d2[' empty '] Trace Back (most recent): ... Keyerror: ' Empty ' >>> d2.empty Traceback (most recent): ... Attributeerror: ' Dict ' object has no attribute ' empty '" " def __init__(Self, * *kw): Super (Dict, self).__init__(**kw)def __getattr__(self, key):Try: returnSelf[key]exceptKeyerror:RaiseAttributeerror (R"' Dict ' object has no attribute '%s '"%key)def __setattr__(self, Key, value): Self[key]=valueif __name__== ' __main__ ': # executes Doctest (can also be run directly in Pycharm) only when run directly on the command line import doctest doctest.testmod ()
Notice the last 3 lines of code. When the module is imported normally, the doctest will not be executed. The doctest is executed only when the command line is running directly. So don't worry that doctest will execute in a non-test environment.
$ Python3 mydict2.py
There's nothing out there. This shows that the doctest we have written is correct. If there is a problem with the program, such as __getattr__()
commenting out the method, then running it will error:
$ python3 mydict2.py**********************************************************************File"/users/michael/github/learn-python3/samples/debug/mydict2.py", Line 10,inch __main__. dictfailed example:d1.xException Raised:traceback (most recent): ... Attributeerror:'Dict'object has no attribute'x'**********************************************************************File"/users/michael/github/learn-python3/samples/debug/mydict2.py", line 16,inch __main__. dictfailed example:d2.cException Raised:traceback (most recent): ... Attributeerror:'Dict'object has no attribute'C'1items had failures:2 of 9inch __main__. DictTest failed*** 2 failures.
Practice
Write doctest to the function fact(n)
and execute:
deffact (n):" ">>> Fact (1) 1 >>> fact (3) 6 >>> fact ( -2) Traceback (most recent call Las T): ... ValueError" " ifN < 1: RaiseValueError ()ifn = = 1: return1returnn * Fact (n-1)if __name__=='__main__': Importdoctest doctest.testmod ()
Python Learning notes (22) Document testing