How to use different parameter combinations to generate a separate testcase function (Python)

Source: Internet
Author: User

There is a problem when using selenium2 python for automated testing, and after writing a testcase report, there is a case execution status record. So we write a login function of the automation use cases, write only one case is obviously not possible, the test case to meet his coverage, so we need to write multiple use cases. But for the same function, our use-case script shows that only the input parameter values are different, and the other operations are the same.

This is a use case to write a test_case_login () script, but we want to record the execution status of each of the cases separately in the report, not to write multiple duplicate methods. such as: Test_case_login_1 () test_case_login_2 () Test_case_login_3 () when the execution is complete, the test report is generated using unittest, and the execution status is recorded for each test_case. But the code is too redundant and the content is too cumbersome. Maybe at this point we can just write a test case and use inline loops, but one problem is that one of them is wrong and it's hard to see it in the test results. The crux of the problem is whether there is a way to generate the corresponding test case based on the different combinations of input parameters. For example, I have 5 groups of data, there should be 5 test_case_login, above I have said that it is not suitable to write 5 test_case_login directly, then what should be done? One possible idea is not to use UnitTest. TestCase the method of the Test_ member function in this class box, instead of writing runtest This member function, there will be some extra work, and it does not look so "smart" if the purpose is to have the framework automatically call Test_case

The natural idea is to use setattr to add member functions to an existing testcase class • To make this approach work, you need to use the static method of the class to generate a member function of the decorate class and return the function to a test function object. Register this call to the Add Test member function somewhere (just before you actually do it, you can do it automatically in the module or you can call it manually) the use case data is as follows:

case_id Summary Login_name Login_password Remark
case_0001 Authentication with NULL user name 111111 Please enter user name
case_0002 Authentication with null password Admin Please enter your password
case_0003 Bad user name Verification Lidje 111111 Sorry, your campus access account and password can not be verified by the system
case_0004 Bad password Authentication Admin Ddsss Sorry, your campus access account and password can not be verified by the system
case_0005 Correct user name, password verification Admin 111111 Login successful
The code is as follows:
  1. 1 #-*-coding:utf-8-*-2  fromSeleniumImportWebdriver3 ImportSYS4Sys.path.append (" Public")5  fromPublicImport Location6  fromPublicImportPlogin126mail7  fromTestImportTest_support8 ImportUnitTest9 ImportxlrdTen Importconfigparser One  A Classlogin126mail (unittest. TestCase): - defsetUp (self): - Print"Start" theSelf.driver =Webdriver. Chrome () -Self.driver.implicitly_wait (30) - #declaring the Find_element method -SELF.FD = Location +Self.verificationerrors =[] - #loading the INI configuration file +CF =Configparser.configparser () ACf.read (".. \\data\\login_126mail_data.ini") at #reading configuration Data -Self.base_url = Cf.get ("urlconf","URL") - #define user name password variables - #self.username = "Auto_tester" - #Self.password = "123qwe" - #Locating keyword Variables inSelf.userinput = Cf.get ("keywords","Userinput") -Self.passinput = Cf.get ("keywords","Passinput") toSelf.btnsubmit = Cf.get ("keywords","btnsubmit") +Self.errorwords = Cf.get ("keywords","Errorwords") -Self.useridwords = Cf.get ("keywords","Useridwords") theSELF.LG = Plogin126mail. Publogin ("Login") *  $ defaction (self, case_id, case_summary, username, password):Panax Notoginseng self.driver.get (Self.base_url) - Self.driver.maximize_window () the PrintU"======== ""+ case_id + u"""+ case_summary + u"=============" + Printusername A PrintPassword the self.lg.login (username, password, self.driver, Self.userinput, Self.passinput, Self.btnsubmit) +spantf =True - Try: $Errortxt =Self.fd.findCss (Self.driver, self.errorwords). Text $spantf =True - except: -spantf =False the ifspantf: - PrintErrortxtWuyi Else: the PrintSelf.driver.title -Self.asserttrue (Self.fd.findId (Self.driver, self.useridwords). Text, U"Login Jump Failed!") Wu  - @staticmethod About defGettestfunc (case_id, case_summary, username, password): $ deffunc (self): - self.action (case_id, case_summary, username, password) - returnfunc -  A  + defTearDown (self): the Print"finished" - self.driver.quit () $ self.assertequal ([], self.verificationerrors) the  the def __generatetestcases(): thedata = Xlrd.open_workbook (u".. \\data\\login_126mail_data.xls") the #get an Excel table by index order -Table =data.sheets () [0] in  forArgsinchRange (1, table.nrows): theRX2 =table.row_values (args) theSetAttr (Login126mail,'test_login126mail_%s'% Txt[0],login126mail.gettestfunc (*txt)) About __generatetestcases() the  the  the defTest_main (): +Test_support.run_unittest (Login126mail)

How to use different parameter combinations to generate a separate testcase function (Python)

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.