To meet previous requirements, run multiple test cases at the same time.
1 #!/usr/bin/env python2 #Coding=utf-83 ImportThreading4 fromMultiprocessingImportQueue5 fromTimeImportCtime,sleep6 fromSubprocessImportPopen,pipe7 ImportOs,time8lock=Threading. Lock ()9 #A single test case generates a temporary report, in the current directory, Result\temp_ days, minutes, minutes, and seconds \ Files directory. HTMLTen #such as E:\python\selenium\fortest\result\temp_20160704_102822\E_python_selenium_fortest_test_1_test1_py.html One defResultfile (tempdir,file): AName=file.replace ('\\','_'). Replace (':',"'). Replace ('.','_')+'. html' - returnOs.path.join (tempdir,name) - classMyThread (Threading. Thread): the def __init__(self,queue,tempresultdir): -Threading. Thread.__init__(self) -Self.queue=Queue -Self.tempresultdir=Tempresultdir + defRun (self): - whileTrue: + if notself.queue.empty (): AFilename=Self.queue.get () at Lock.acquire () -Resultname=resultfile (self.tempresultdir,filename) -Cmd="python"+filename+" "+Resultname - #Print cmd - Print 'Start time:%s'%CTime () - lock.release () inP=popen (cmd,shell=true,stdout=PIPE) - #If you do not add the following print, you will not wait for execution to PrintP.stdout.readlines () + Else: - Print 'End' the Break * #gets the file at the beginning of the path under test, starting with test. py End Files $ defgetfile (path):Panax Notoginsengpaths=[] - forPinchOs.listdir (path): the ifp[0:4]=='Test' andOs.path.isdir (p): + paths.append (P) Afile=[] the forPinchpaths: +temp=Os.path.join (path,p) - #Print Temp $files=Os.listdir (temp) $ #Print Files - forFinchFiles: - iff[0:4]=='Test' andf[-3:]=='. PY': the file.append (Os.path.join (temp,f)) - returnfileWuyi if __name__=='__main__': the Print 'Main start time:%s'%CTime () -Tempresultdir=os.path.join (OS.GETCWD (),"result","Temp"+time.strftime ("_%y%m%d_%h%m%s", Time.localtime (Time.time ()))) Wu Os.mkdir (Tempresultdir) -Resultreport=os.path.join (OS.GETCWD (),"result"+time.strftime ("_%y%m%d_%h%m%s", Time.localtime (Time.time ()))) Aboutallfile=GetFile (OS.GETCWD ()) $Queue=Queue () - forFileinchAllfile: - queue.put (file) -my_threads=[] AMy_thread=Threading. Thread () + forIinchRange (2): theMy_thread=MyThread (Queue,tempresultdir) -my_thread.deamon=True $ my_threads.append (My_thread) the My_thread.start () the forTinchmy_threads: the T.join () the -reports=Os.listdir (Tempresultdir) in Printreports the Print 'Main End time:%s'%ctime ()
A single test case is written as follows, just an example:
1 #-*-coding:utf-8-*-2 ImportUnittest,time,sys3Sys.path.append ("..") 4 fromPublicImportHtmltestrunner5 classTest (unittest. TestCase):6 defsetUp (self):7 Pass 8 defTest_login (self):9U"""test_1 test1 Login use case login""" TenTime.sleep (5) One Pass A deftest_process (self): -U"""test_1 test1 Processing use case login""" -Time.sleep (5) theAssertequal () - defTest_quit (self): -U"""test_1 test1 Login use case quit""" -Time.sleep (5) +Self.assertequal () - defTearDown (self): + Pass A if __name__=='__main__': atsuit=UnitTest. TestSuite () -Filename=sys.argv[1] - suit.addtest (unittest.makesuite (test)) -F=open (filename,"WB") -Runner =Htmltestrunner.htmltestrunner ( -stream=F, inTitle=u'Test Report', -Description=u'Test Results') toRunner.run (SUIT)
Results of execution:
Find a problem if you remove the code from the
Print ' Start time:%s ' %ctime ()
Two threads do not continue until the execution is complete
Directory structure:
│ testlibmain.py│ thread.py│ threadtest.py│ ├─public│ htmltestrunner.py│ __init__.py│ ├─result│ └─temp_20160704_102848│ e_python_selenium_fortest_test_1_test1_py.html│ E_python_ selenium_fortest_test_1_test2_py.html│ e_python_selenium_fortest_test_2_test1_py.html│ E_python_ selenium_fortest_test_2_test2_py.html│ e_python_selenium_fortest_test_3_test1_py.html│ E_python_ selenium_fortest_test_3_test2_py.html│ ├─test_1│ test1.py│ test2.py│ __init__.py│ ├─test_2│ test1.py│ test2.py│ __init__.py│ └─test_3 test1.py test2.py __ init__.py
One of the test reports:
The above code does not have a parameter binding to the Webdriver address, and can be added later
Multiple report files generated eventually need to be merged into one, subsequent re-write
Python Multithreading executes test cases and generates multiple reports