Objective
Suppose you execute a script (. py) use case for a minute, then 100 scripts take 100 minutes, and when your use case reaches 1000, it takes 1000 minutes, or 16 hours ...
How can you save time by running multiple. py scripts in parallel? This is the use of multi-threaded, theoretically open 2 threads time to save half, open 5 threads, time is shortened by five times times.
Project structure
1. The project structure is the same as the previous design:
- The. Py use case script at the beginning of cases test
- Common put public modules, such as Htmltestrunner
- Report-Generated HTML reports
- run_all.py for executing all scripts
2.case folder inside use case reference
#Coding:utf-8ImportUnitTest fromSeleniumImportWebdriverImport TimeclassTest1 (unittest. TestCase): @classmethoddefSetupClass (CLS): Cls.driver=Webdriver. Firefox ()defsetUp (self): Self.driver.get ("http://www.cnblogs.com/yoyoketang/") deftest_01 (self): Time.sleep (3) T=Self.driver.titlePrintT#casually written use cases, not written assertions deftest_02 (self): Time.sleep (3) T=Self.driver.titlePrintt h=Self.driver.window_handlesPrinth#casually written use cases, not written assertions@classmethoddefTeardownclass (CLS): Cls.driver.quit ()if __name__=="__main__": Unittest.main ()
Multithreaded execution
1. Multithreading Design ideas:
- Write a function of run first
- Ensure that the For loop can run through
- Add an adorner to the Run function @threads (n), n is the number of threads
2.run_all Reference Code
#Coding=utf-8ImportUnitTest fromCommonImportHtmltestrunnerImportOS fromTomorrowImportThreads#Python2 need these three lines, Python3 don't needImportsysreload (SYS) sys.setdefaultencoding ('UTF8')#Get PathCurpath = Os.path.dirname (Os.path.realpath (__file__)) Casepath= Os.path.join (Curpath," Case") Reportpath= Os.path.join (Curpath," Report")defAdd_case (Case_path=casepath, rule="test*.py"): " "load all the test cases" "Discover=Unittest.defaultTestLoader.discover (Case_path, pattern=rule, Top_level_dir.=None)returnDiscover@threads (3)defRun_case (All_case, Report_path=reportpath, nth=0):" "execute all the use cases and write the results to the test report" "Report_abspath= Os.path.join (Report_path,"result%s.html"%nth) FP= Open (Report_abspath,"WB") Runner= Htmltestrunner.htmltestrunner (stream=FP, title=u'automated test Reports, test results are as follows:', Description=u'use case execution:') #Call the Add_case function return valueRunner.run (all_case) fp.close ()if __name__=="__main__": #use case CollectionsCases =add_case ()#before the batch execution, this is changed to a for loop execution forI, JinchZip (cases, range (Len list (cases))): Run_case (i, Nth=J)#Execute use cases, generate reports
3. Generate reports, where the generated reports are multiple, each. py script generates an HTML report, and the next challenge is to merge the reports
How do I combine multiple HTML reports into a single report?
Selenium+python Automated 90-unittest multithreaded execution cases