Python module: doctest,unitest module

Source: Internet
Author: User

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

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.