"""
This file provide an example about doctest
>>> reverse_data('abcdefg')
'gfedcba'
"""defreverse_data(origin_data):"""Return the reverse of original data.
>>> reverse_data('hello world')
'dlrow olleh'
>>> reverse_data([1, 3, 5, 7, 9])
[9, 7, 5, 3, 1]
>>> reverse_data((1, 2, 3, 4, 5))
(5, 4, 3, 2, 1)
>>> reverse_data({1: "a", 2: "b", 3: "c"})
Traceback (most recent call last):
...
TypeError: unhashable type: 'slice'
>>> reverse_data({1, 3, 5})
Traceback (most recent call last):
...
TypeError: 'set' object is not subscriptable
"""returnorigin_data[::-1]literal_docstring="""
>>> reverse_data('12345')
'54321'
"""__test__={'a literal docstring':literal_docstring}if__name__=="__main__":importdoctestdoctest.testmod()
执行结果相较于之前,会增加一个测试结果:
1
2
3
4
5
6
7
8
...
3 items passed all tests:
1 tests in test_doctest
1 tests in test_doctest.__test__.a literal docstring
5 tests in test_doctest.reverse_data
7 tests in 3 items.
7 passed and 0 failed.
Test passed.
"""
This file provide an example about doctest
>>> str3 = 'test3'
>>> reverse_data(str3)
'3tset'
"""str1='test1'defreverse_data(origin_data):"""Return the reverse of original string.
>>> str2 = 'test2'
>>> reverse_data(str1)
'1tset'
>>> reverse_data(str2)
'2tset'
>>> reverse_data(str3)
'3tset'
"""returnorigin_data[::-1]if__name__=="__main__":importdoctestdoctest.testmod()
Trying:
str3='test3'
Expecting nothing
ok
Trying:
reverse_data(str3)
Expecting:
'3tset'
ok
Trying:
str2='test2'
Expecting nothing
ok
Trying:
reverse_data(str1)
Expecting:
'1tset'
ok
Trying:
reverse_data(str2)
Expecting:
'2tset'
ok
Trying:
reverse_data(str3)
Expecting:
'3tset'
**********************************************************************
File "/home/yinjunz/test_python/test_doctest.py", line 18, in test_doctest.reverse_data
Failed example:
reverse_data(str3)
Exception raised:
Traceback (most recent call last):
File "/usr/local/python3.7.9/lib/python3.7/doctest.py", line 1337, in __run
compileflags, 1), test.globs)
File "<doctest test_doctest.reverse_data[3]>", line 1, in <module>
reverse_data(str3)
NameError: name 'str3' is not defined
1 items passed all tests:
2 tests in test_doctest
**********************************************************************
1 items had failures:
1 of 4 in test_doctest.reverse_data
6 tests in 2 items.
5 passed and 1 failed.
***Test Failed*** 1 failures.