Parameterization of UnitTest data

Source: Internet
Author: User

The unittest itself does not support parameterization, but other methods can be used

1, unittest using 3rd party plug-in parameterized for parameterization

First, type the following command in DOS to install parameterized
python-m pip Install parameterized

Guide Package:
From parameterized import parameterized

Cases:

1 ImportUnitTest2  fromPylibrary. PylibImport*3 4 classInterface_report2 (unittest. TestCase):5 6 #Getting started with parameterized7@parameterized. Expand ([#referencing adorners, preparing parameters, passing in functions below8("2018-09-26",'2018-09-26',"Test","2001", 49,'Totalcallnum'),9("2018-09-26",'2018-09-26',"Test","2001", 60,'Totalcallanswerednum'),Ten("2018-09-26",'2018-09-26',"Test","2001", 60,'Ringnum'), One("2018-09-13",'2018-09-13',"Test","2001", 4,'Totalcallnum_transfer'), A     ]) -  -  the     defTest_agreport1 (Self,start_time,end_time,template_name,ag_num,testdata,reportdata):#when executed to this will automatically receive the parameters passed in above -Self.num =TestData -SELF.REPORT_NUM1 =pylib (). Getagreport (Start_time,end_time,template_name,ag_num) -self.report_num=Self.report_num1[0][reportdata] + self.assertequal (self.num,self.report_num) -  + if  __name__=='__main__': A  atUnittest.main (verbosity=2) -  -#verbosity表示测试结果的信息复杂度, using 0,1,2 respectively, its specific meaning is as follows: -#0 (silent mode): Just get the total number of test cases and the total results -#1(default mode): At this point there is a '. ' in front of each successful use case, with a ' F ' in front of each failed use case -#2 (verbose mode): test results show all relevant information for each test case

2. UnitTest DDT Library

Pip Install DDT

First create a data file: Test_ddt_file.json

1 {2     "test_agreport1_01":{3         "start_time":"2018-09-26",4         "End_time":"2018-09-26",5         "Template_name":"Test",6         "Ag_num":"2001",7         "TestData": 49,8         "ReportData":"Totalcallnum"9     },Ten     "test_agreport1_02":{ One         "start_time":"2018-09-26", A         "End_time":"2018-09-26", -         "Template_name":"Test", -         "Ag_num":"2001", the         "TestData": 60, -         "ReportData":"Totalcallanswerednum" -     }, -     "test_agreport1_03":{ +         "start_time":"2018-09-26", -         "End_time":"2018-09-26", +         "Template_name":"Test", A         "Ag_num":"2001", at         "TestData": 60, -         "ReportData":"Ringnum" -     }, -     "test_agreport1_04":{ -         "start_time":"2018-09-13", -         "End_time":"2018-09-13", in         "Template_name":"Test", -         "Ag_num":"2001", to         "TestData": 4, +         "ReportData":"Totalcallnum_transfer" -     } the}

To create a test case:

1 ImportUnitTest2  fromPylibrary. PylibImport*3  fromDdtImportDDT, File_data4 5 6 @ddt7 classInterface_report2 (unittest. TestCase):8 9@file_data ("D:\\python_code\\test_ddt_file.json")Ten     defTest_agreport1 (self,start_time,end_time,template_name,ag_num,testdata,reportdata): OneSelf.num =TestData ASELF.REPORT_NUM1 =pylib (). Getagreport (Start_time,end_time,template_name,ag_num) -self.report_num=Self.report_num1[0][reportdata] - self.assertequal (self.num,self.report_num) the  -  -  - if  __name__=='__main__': +  -Unittest.main (verbosity=2)

Parameterization of UnitTest data

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.