1. Test the code with the Doctest module
[Email protected] tmp]# vim qwe.py
#!/bin/env python
#!-*-Coding:utf-8-*-
DEF ABC ():
Print 4;
[Email protected] tmp]# vim doctest.py
#!/bin/env python
#!-*-Coding:utf-8-*-
Import doctest,qwe #加载doctest模块, loading qwe file name
If __name__== "__main__":
Doctest.testmod (Qwe)
[email protected] tmp]# python doctest.py #检查python语法
[email protected] tmp]# python doctest.py-v #查看代码的执行过程
2 Items had no tests:
Qwe
Qwe.abc
0 tests in 2 items.
0 passed and 0 failed.
Test passed.
[Email protected] tmp]#
2. Test the code with the UnitTest module
The Unittest.main () function is responsible for actually running the test. It instantiates all the testcase subclasses and runs all the names in the method of the test method.
[Email protected] tmp]# vim unittest.py
#!/bin/env python
#!-*-Coding:utf-8-*-
Import UnitTest
Class Tong (unittest. TestCase): #继承unittest模块
def testa (self): #函数名必须test开头
print ' A '
def testb (self): #函数名必须test开头
print ' B '
If __name__== "__main__":
Unittest.main () #检查所有以TestCase类, whether the function at the beginning of test has an exception
[email protected] tmp]# python doctest1.py
A
. b
.
----------------------------------------------------------------------
Ran 2 Tests in 0.000s
Ok
[Email protected] tmp]#
This article is from the "Days Together" blog, please be sure to keep this source http://tongcheng.blog.51cto.com/6214144/1766734
Python module: doctest,unitest module