In order to solve the problem that all the test cases under the files in the UnitTest test were executed and the test reports were generated.
The scheme is as follows:
Directory structure
│testlibmain.py
│
├─public
│htmltestrunner.py
│
├─result
├─test_1
│test1.py
│test2.py
│
├─test_2
│test1.py
│test2.py
│
└─test_3
test1.py
test2.py
Naming conventions, test case folders, and files begin with test, the class name of the testing case is uniform to test
Save test report in common Lib,result saved in public
Idea: Through the current directory of the __init__.py file implementation of all folders under the class import, the file is dynamically generated, traverse the current folder all folders, get the test case file name
#-*-Coding:utf-8-*-
Import Sys,os,time
Import UnitTest
def getdir (Currentpath,allpath):
result = []
For F in Allpath:
tmp = currentpath+ ' \ \ ' +f
if (Os.path.isdir (TMP)):
Result.append (f)
return result
def getfilename (Dirpath):
File=os.listdir (Dirpath)
Filename=[]
For f in file:
if (f[0:4]== ' test ' and f[-3:]== '. Py '):
Filename.append (f[0: (Len (f)-3)])
return filename
def genemptyinit (Dirpath):
Fp=open (dirpath+ ' \\__init__.py ', "w")
Fp.flush ()
Fp.close ()
Def genallinit ():
Fp=open ("__init__.py", "W")
Fp.write ("#-*-coding:utf-8-*-\n")
Fp.write ("Import sys\n")
Fp.write ("From time import sleep\n")
CURRENTPATH=OS.GETCWD ()
Allfile=os.listdir (Currentpath)
Dir=getdir (Currentpath,allfile)
Testcase=[]
For D in dir:
Genemptyinit (d)
Filenames = GetFileName (d)
For F in filenames:
Fp.write (' From ' +d+ ' import ' + f + ' \ n ')
Testcase.append (d+ '. ') +f+ '. ' + ' Test ')
Fp.write (' Import unittest\n ')
Fp.flush ()
Fp.close ()
Return testcase
testcases = Genallinit ()
Sys.path.append ("..")
From fortest Import *
From public import Htmltestrunner
Suit=unittest. TestSuite ()
For Test in testcases:
Suit.addtest (Unittest.defaultTestLoader.loadTestsFromName (test))
filename = ' E:\\python\\selenium\\fortest\\result\\result ' + time.strftime ("_%y%m%d_%h%m%s", Time.localtime ( Time.time ()) + '. html '
Fp=open (filename, "WB")
Runner=htmltestrunner.htmltestrunner (
stream = FP,
title = U ' Test report ',
Description = U ' use case execution result ')
Runner.run (SUIT)
How to use test cases
#-*-Coding:utf-8-*-
Import UnitTest
Class Test (UnitTest. TestCase):
def setUp (self):
Pass
def test_login (self):
U "" "Test_1 test1 login use case Login" "
Pass
def test_quit (self):
U "" "Test_1 test1 login use case quit" ""
Pass
def tearDown (self):
Pass
The test report is as follows:
The framework of multithreaded execution cases is being continued ...
Python unittest Test framework 1 (single thread sequential execution)