foreword
Assuming that one script (.py) use case is executed for one minute, then 100 scripts will take 100 minutes, and when your use case reaches a thousand, it will take 1000 minutes, which is more than 16 hours. . .
So how to run multiple .py scripts in parallel and save time? This uses multi-threading. In theory, opening 2 threads saves half the time, and opening 5 threads reduces the time by five times.
Project structure
1. The project structure is the same as the previous design:
- .py use case script at the beginning of case test
- common put public modules, such as HTMLTestRunner
- report put the generated html report
- run_all.py is used to execute all scripts
2. Use case reference in the case folder
# coding:utf-8 import unittest from selenium import webdriver import time class Test1(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = webdriver.Firefox() def setUp(self): self.driver.get("http://www.cnblogs.com/yoyoketang/") def test_01(self): time.sleep(3) t = self.driver.title print t #Use cases written casually, no assertions written def test_02(self): time.sleep(3) t = self.driver.title print t h = self.driver.window_handles print h #Use cases written casually, no assertions written @classmethod def tearDownClass(cls): cls.driver.quit() if __name__ == "__main__": unittest.main()
multithreaded execution
1. Multi-threaded design ideas:
- First write a run function
- Make sure the for loop works
- Add a decorator @threads(n) to the run function, where n is the number of threads
2.run_all reference code
# coding=utf-8 import unittest from common import HTMLTestRunner import os from tomorrow import threads # python2 needs these three lines, python3 does not need import sys reload(sys) sys.setdefaultencoding('utf8') #Get the path curpath = os.path.dirname(os.path.realpath( __file__ )) casepath = os.path.join(curpath, "case") reportpath = os.path.join(curpath, "report") def add_case(case_path=casepath, rule= " test*.py " ): ''' Load all test cases ''' discover = unittest.defaultTestLoader.discover(case_path, pattern=rule, top_level_dir=None) return discover @threads( 3 ) def run_case(all_case, report_path=reportpath, nth= 0): ''' Execute all 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 report, the test results are as follows: ' , description =u ' Use case execution: ' ) #Call the add_case function to return the value runner.run(all_case) fp.close() if __name__ == " __main__ " : #use case collection cases = add_case() #Before it was executed in batches, here it is changed to a for loop to execute for i, j in zip(cases, range(len(list(cases)))): run_case(i, nth =j) #Execute the case and generate a report
3. Generate reports. There are multiple reports generated here. Each .py script generates an html report. The next difficulty is to merge the reports.
How to combine multiple html reports into one report?