Objective
Paramunittest is a dedicated module for unittest implementation parameterization, which can be passed into multiple sets of parameters and automatically generate multiple use cases
In front of the data-driven, DDT can be used to solve multiple sets of data incoming, automatic generation of multiple test cases. This article continues to introduce another parameterized frame paramunittest, which can achieve the same effect.
Environment preparation
1. First pip download install Paramunittest
Pip Install Paramunittest
Official documents
1. Official Document Address: https://pypi.python.org/pypi/ParamUnittest/
2.github Source: Https://github.com/rik0/ParamUnittest
3. The official documentation gives 2 use cases, the following two methods are possible, small series tend to the second, easier to understand some, here note, in the pycharm inside the run, the last mouse to be placed under the if execution.
4. Add this parameter verbosity=2 to show details of use case execution
verbosity=2
Import UnitTestImport Paramunittest# Scenario One@paramunittest. parametrized (' 1 ',' 2 '),# (4, 3), (' 2 ',' 3 '), ((' 4 ',), {' B ':' 5 '}), ((), {' A ':5,' B ':6}), {' A ':5,' B ':6},)ClassTestfoo(Paramunittest. Parametrizedtestcase):DefSetparameters(Self, A, b): SELF.A = a self.b = bDefTestless(self): self.assertless (SELF.A, self.b)# Scenario Two@paramunittest. parametrized (' 1 ',' 2 '),# (4, 3), (' 2 ',' 3 '), ((' 4 ',), {' B ':' 5 '}), ((), { ' a ': 5, ' B ': 6}), { ' a ': 5, ' B ': 6},) class testbar def setparameters< Span class= "Hljs-params" > (self, A, b): SELF.A = a self.b = b def Span class= "Hljs-title" >testlessif __name__ = = " __main__ ": Unittest.main (verbosity= 2)
Practice Cases
1. From the case of the official document above, it can be seen that parameters can be passed to the tuple can also be passed to the dictionary, first pass the dictionary parameters, similar to the following set of parameters:
{"User": "admin", "PSW": "123", "Result": "True"}
2. Note that when the parameter is accepted, the Setparameters method must be defined and can only be this name. The arguments following the parentheses accept the passed-in parameter names. The dictionary is defined earlier, and the parameter is consistent with the key of the previous dictionary.
Import UnitTestImport ParamunittestImport time# python3.6# Shanghai-Leisurely@paramunittest. parametrized ({"User":"Admin","PSW":"123","Result":"True"}, {"User":"Admin1","PSW":"1234","Result":"True"}, {"User":"Admin2","PSW":"1234","Result":"True"}, {"User":"Admin3","PSW":"1234","Result":"True"}, {"User":"Admin4","PSW":"1234","Result":"True"}, {"User":"Admin5","PSW":"1234","Result":"True"}, {"User":"Admin6","PSW":"1234","Result":"True"}, {"User":"Admin7","PSW":"1234","Result":"True"}, {"User":"Admin8","PSW":"1234","Result":"True"}, {"User":"Admin9","PSW":"1234","Result":"True"}, {"User":"Admin10","PSW":"1234","Result":"True"}, {"User":"Admin11","PSW":"1234","Result":"True"},)ClassTestdemo(UnitTest. TestCase):Defsetparameters(self, user, PSW, result): "Note here that the user, PSW, result three parameters and the dictionary one by one defined above correspond to" Self.user = user Self.user = PSW Self.result = result def testcase(self): print ("Start execution case:--------------") Time.sleep ( 0.5) Print ("Enter user name:%s"% self.user) print ("Enter password:%s"% self.user) print ("Expected Result:%s"% Self.result) Time.sleep ( 0.5) self.asserttrue (Self.result = = "true")if __name__ = = "__main__": Unittest.main (verbosity= 2)
3. Implementation results:
To begin the execution of a use case:--------------TestCase (paramunittest. TESTDEMO_0) ... Enter user name:123 Enter Password:Results of 123-year hope:True Oktestcase (paramunittest. Testdemo_1) ... To begin the execution of a use case:--------------Enter the user name:1234 Enter Password:Results of 1234th-year hope:True Oktestcase (paramunittest. TESTDEMO_10) ... To begin the execution of a use case:--------------Enter the user name:1234 Enter Password:Results of 1234th-year hope:True to begin execution of the use case:--------------Oktestcase (paramunittest. Testdemo_11) ... Enter user name:1234 Enter Password:Results of 1234th-year hope:True to begin execution of the use case:--------------Oktestcase (paramunittest. Testdemo_2) ... Enter user name:1234 Enter Password:Results of 1234th-year hope:True OK to start execution of the use case:--------------TestCase (paramunittest. Testdemo_3) ... Enter user name:1234 Enter Password:Results of 1234th-year hope:True Oktestcase (paramunittest. Testdemo_4) ... To begin the execution of a use case:--------------Enter the user name:1234 Enter Password:Results of 1234th-year hope:True Oktestcase (paramunittest. Testdemo_5) ... To begin the execution of a use case:--------------Enter the user name:1234 Enter Password:Results of 1234th-year hope:True to begin execution of the use case:--------------Oktestcase (paramunittest. Testdemo_6) ... Enter user name:1234 Enter Password:1234 expected Result: true Start execution use case: ---------- ----Oktestcase (paramunittest. testdemo_7) ... Enter user name: 1234 Enter password: 1234 desired result: true OK start execution use case: --------------testcase (paramunittest. Testdemo_8) ... Enter user name: 1234 Enter password: 1234 desired result: true Oktestcase (paramunittest. Testdemo_9) ... Start execution use case: --------------Enter user name: 1234 Enter password: 1234 expected Result: true ok--------------- -------------------------------------------------------Ran 12 tests in 12.001s
4. Note that the order of execution here is to execute 0,1 first, then execute 10,11,12 in turn, do not ask me why, do not find me to solve, design so, before the DDT framework has the same problem.
5. In addition to the dictionary parameters, the type of the meta-group is also possible
@paramunittest. parametrized ("Admin","123","true"), ("Admin1","123","true"), ("Admin2","123","true"), ("Admin3","123","true"), ("Admin4","123","true"), ("Admin5","123", "true"), ("admin6", "123", "true"), ("admin7", "123", "true"), ("Admin8", "123", "true"), ("admin9", "123", "true"), ("Admin10", "123", "true"), (" Admin11 ", " 123 ", " true "), (" Admin12 ", " 123 ", " true "))
Generating HTML reports
1.unittest generated HTML report reference earlier described in this article http://www.cnblogs.com/yoyoketang/p/6680503.html, here is not repeated introduction, the implementation of the following:
Selenium+python Automation 97--unittest parameterization (paramunittest)