Python module UnitTest

Source: Internet
Author: User

Python comes with a unit test framework, UnitTest module, which is used to do unit testing, which encapsulates some of the methods of verifying the return results and some initialization operations before the use cases are executed.

Before you say UnitTest, say a few concepts:

TestCase: Test Cases

TestSuite: Multiple test cases are assembled together

Testloader: Used to load testcase to Testsuite

Testrunner: Execute the test case, the results of the test are saved to the TestResult instance, including how many test cases were run, how many succeeded, how many failed, etc.

Import Unittestclass MyTest (unittest. TestCase):  # Inherit UnitTest. TestCase    def tearDown (self):        # After each test case is executed        print (' 111 ')    def setUp (self):        # Before each test case executes        print (' 22222 ')    @classmethod    def teardownclass (self):    # must use the @ Classmethod adorner, run         once after all test runs Print (' 4444444 ')    @classmethod    def setupclass (self):    # must use @classmethod adorner, all test runs before run once        Print (' 33333 ')    def test_a_run (self):        self.assertequal (1, 1)  # test Case            def test_b_run (self):        Self.assertequal (2, 2)  # test Case        If __name__ = = ' __main__ ':    unittest.main () #运行所有的测试用例

Here are some common assertions, that is, verifying the results

        Assertequal (A, b)     a = = B              assertnotequal (A, b)     A! = B              asserttrue (x)     bool (x) is True              Assertfalse (x)     BOOL (x) is False              assertisnone (x)     x is None             assertisnotnone (x)     x are not none           Assertin (A, B)     A in B            Assertnotin (A, b)     a not in B    

Htmltestrunner module, you can generate test reports, this module needs to install itself, using the execution of test cases will generate an HTML test report, there will be the results of each test case execution,

Import Htmltestrunner Import UnitTest class MyTest (unittest. TestCase): #继承unittest. TestCase def tearDown (self): #每个测试用例执行之后做操作 print (' 111 ') def setUp (sel f): #每个测试用例执行之前做操作 print (22222) def test_run (self): # Self.asserte Qual (self.assertis) #测试用例 def test_run2 (self): # Self.ass Ertequal (self.assertis) #测试用例 def test_run3 (self): # self . Assertequal (Self.assertis) #测试用例 def test_run1 (self): #            Self.assertequal (Self.assertis) #测试用例 if __name__ = = ' __main__ ': Test_suite = UnitTest. TestSuite () #创建一个测试集合 test_suite.addtest (MyTest (' test_run1 ')) #测试套件中添加测试用例 #test_suite. Addtest (unittes T.makesuite (MyTest))#使用makeSuite方法添加所有的测试方法 fp = open (' res.html ', ' WB ') #打开一个保存结果的html文件 runner = Htmltestrunner.htmltestr Unner (stream=fp,title= ' API test Report ', description= ' test case ') #生成执行用例的对象 Runner.run (test_suite) #执行 Test suite

If we have more than one module, each module has written a lot of Python files, each python file has a test case, how to put the use case in this directory is executed, that is, to find all the Python files in this directory, and then find the inside of the test cases, executed

Import Unittest,htmltestrunner        suite = unittest. TestSuite () #创建测试套件        all_cases = Unittest.defaultTestLoader.discover ('. ', ' test_*.py ')        # Locate all the test cases in the Python file that begin with test in a directory in the        all_cases:            suite.addtests (case) #把所有的测试用例添加进来        fp = open (' Res.html ', ' WB ')        runner = Htmltestrunner.htmltestrunner (stream=fp,title= ' all_tests ', description= ' all test Cases ')        Runner.run (Suite)        #运行测试

When we continue to integrate in the future, to let the code run automatically, we will use Jenkins, but the results of the test report is HTML format, Jenkins do not know, in the Jenkins inside the display does not come out. That would generate a test report that Jenkins knew, and Jenkins knew the report in XML format, and that produced the report in XML format, which required a new module, Xmlrunner, to install the direct pip install Xmlrunner

Import Unittestimport xmlrunner# Imports This module class My (UnitTest. TestCase):     def test1 (self,a,b,c):        self.assertequal (a+b,c) if __name__== ' __main__ ':    test_suite = UnitTest. TestSuite ()    test_suite.addtest (Unittest.makesuite (My))    runner = Xmlrunner. Xmltestrunner (output= ' report ') #指定报告放的目录    runner.run (test_suite)

Once run, a test report in XML format is generated in the reports directory, and the date is added automatically

Reproduced

Python module UnitTest

Related Article

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.