Python unittest Test framework 1 (single thread sequential execution)

Source: Internet
Author: User

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)

Related Article

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.