Selenium+python Automated 90-unittest multithreaded execution cases

Source: Internet
Author: User

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

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.