Python uses closures to dynamically generate unittest execution scripts---Analyze problems, solve problems, and record pits.

Source: Internet
Author: User

As for why to dynamically generate UnitTest execution scripts, as well as design ideas, code reading refer to the previous article, Portal. Well, this article we on the previous article of the problem to do a bit of analysis, record the road of pits, this pit akzent deep, struggled for a long time to come out.

First let's talk about the problem, because the closure, the closure will bind the variable cannot be changed, if I first run the Testall function loop two times, the first is normal, the second run Testall function loop once, then the generated report will have problems, The result of the first run occurs. That is: The last cycle of the operation as long as there is less than the previous cycle, then the report must appear the previous results, must first of the bug.

All right, let's talk about this. How deep is the pit of dynamic generation UnitTest execution scripts, mainly involving closures, and because closure loops call UnitTest's test class to add a method that makes up the suite, we all know that Python's closures bind variables, cannot be erased, Let's put the relevant code on it for easy viewing.

1 classTest (unittest. TestCase):#TestCase class2     "Test Class"3 4     defBegin_req (self, apidata):#class Method5back = Run_single_testcase (apidata)#Call Request6YQ = 200#Results Comparison7SJ =Back.status_code8 self.assertequal (SJ, YQ)9 Ten defDemo (Apidata):#Closed Package One     deftool (self): ATest.begin_req (self, apidata)#Call the test method -  -SetAttr (Tool,'__doc__', u'Test%s'% Str (apidata['name'])) the     returnTool -  - defTestall (Apidata):#Loop Call Closure -      forIinchRange (len (apidata)): +SetAttr (Test,'Test_'+ STR (i + 1), demo (Apidata[i])) -  + defSuite (Apidata):#Call Testall assembled test class, assemble suit A Testall (apidata) atSuit =unittest.makesuite (Test) -     returnSuit

Code definition: Class Test inheritance Unittest.testcase There's nothing to say, the first parameter of the Tool,tool method of the demo closure inner function is the test class (I understand that the pointer points to the test Class), the function Testall loops the closure, Function Suite I've got a very clear comment on it.

This pit has a lot of confusion, I have a lot of time to focus on the closure function, I think it is the closure of the cause, has been studying the closure where the error, and how to change the parameters of the closure function, with a lot of methods, what container parameters, after the deletion of the container members what, the full use of ... MMP ... The results are actually closures, but the problem is not in the closure function, to understand the closure please refer to other articles, portals, we will not vernacular, the code to solve

1 defTestall (apidata):2NameList = []3      forIinchRange (len (apidata)):4Name ='Test_'+ STR (i + 1)5 setattr (Test, Name, demo (Apidata[i]))6 namelist.append (name)7     returnNameList8 9 defSuite (apidata):TenNameList =Testall (apidata) OneSuites =UnitTest. TestSuite () A      forIinchNameList: - suites.addtest (Test (i)) -     returnSuites

The main changes are the Testall function and the suite function, in fact the problem is in the suite function, the original function is the following code

1 def Suite (apidata):  #  call Testall Assemble test class, assemble suit2    testall (apidata)  3     suit = unittest.makesuite (Test)4     return suit

The third line of the Makesuite function, refers to the one-time Add test class method to the suite, the result of the closure bound object (Python all objects) in test, the first run of the assembly test class method is closed binding, the next run if less than the previous, Then there will be no overridden method, there will be errors, so modify the Makesuite function for the Addtest function, the problem is solved, I studied for a long time closure ah, the results of the problem in the test class, heart tired. The writing is too poor. Excuse me.. Thanks a lot..

Python uses closures to dynamically generate unittest execution scripts---Analyze problems, solve problems, and record pits.

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.