Python Learning Note (14): UnitTest

Source: Internet
Author: User

Python has a self-contained unit test framework that is the UnitTest module, which is used for unit testing, which encapsulates some of the result methods returned by the checksum and some initialization operations before the use cases are executed.

Before you say UnitTest, say a few concepts:

TestCase is also a test case

TestSuite Multiple test cases are assembled together, that is TestSuite

Testloader is used to load testcase into Testsuite.

Testrunner is to execute a test case, the results of the test are saved to the TestResult instance, including how many test cases were run, how much success, how many failed, etc.

Write a simple unit test case below

        ImportUnitTestclassMyTest (unittest. TestCase):#inherit UnitTest. TestCase            defTearDown (self):#take action after each test case executes                Print('111')            defsetUp (self):#do actions before each test case executes                Print(22222)            defTest_run (self): self.assertequal (The)                #Test Cases                        if __name__=='__main__': Unittest.main ()#Run all the test cases

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  is  not   None Assertin (A, b) a  i n   b Assertnotin (A, b) a  not  in  b 

How to generate a test report, you need to add another module, Htmltestrunner, 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, the code is as follows:

        ImportHtmltestrunnerImportUnitTestclassMyTest (unittest. TestCase):#inherit UnitTest. TestCase            defTearDown (self):#take action after each test case executes                Print('111')            defsetUp (self):#do actions before each test case executes                Print(22222)            defTest_run (self):#self.assertequal (a)Self.assertis ()                #Test Cases            deftest_run2 (self):#self.assertequal (a)Self.assertis ()                #Test Cases            deftest_run3 (self):#self.assertequal (a)Self.assertis ()                #Test Cases            deftest_run1 (self):#self.assertequal (a)Self.assertis ()                #Test Cases        if __name__=='__main__': Test_suite= UnitTest. TestSuite ()#Create a Test collectionTest_suite.addtest (MyTest ('test_run1'))#Add test Cases to the test suite            #test_suite.addtest (Unittest.makesuite (MyTest)) #使用makeSuite方法添加所有的测试方法fp = open ('res.html','WB')#Open an HTML file that saves the resultsRunner = Htmltestrunner.htmltestrunner (stream=fp,title='API Test Report', description='Test Situation')            #the object that generated the execution caseRunner.run (test_suite)#executing a test suite

If we have a lot of modules, each module is written under a lot of Python files, each python file has a test case, how to put the use case in this directory is executed, we must first find all the Python files in this directory, and then find the inside of the test cases, executed, The code is as follows:

       ImportUnittest,htmltestrunner Suite= UnitTest. TestSuite ()#Create a test suiteAll_cases = Unittest.defaultTestLoader.discover ('.','test_*.py')        #Find all the test cases in a directory that contain all the Python files that begin with Test         forCaseinchall_cases:suite.addTests (case)#Add all the test casesfp = open ('res.html','WB') Runner= Htmltestrunner.htmltestrunner (stream=fp,title='all_tests', description='all test conditions') Runner.run (suite)#Run Tests

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. Then we have to produce some Jenkins know the test report, Jenkins know the XML format of the report, then we produce the XML format, we need to use a new module, Xmlrunner, install the direct pip install Xmlrunner can be, the code is as follows:

ImportUnitTestImportXmlrunner#Import this moduleclassMy (unittest. TestCase):deftest1 (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')#Specify the directory where the report is placedRunner.run (Test_suite)

Python Learning Note (14): UnitTest

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.