直接上代码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# -*- coding: utf-8 -*-
import time
import os
import os.path
import re
import unittest
import HTMLTestRunner
import shutil
shutil.copyfile( "setting.ini" , "../setting.ini" )
casepaths = []
def createsuite(casepath):
testunit = unittest.TestSuite()
#discover方法定义
discover = unittest.defaultTestLoader.discover(
casepath,
pattern = 'case*.py' ,
top_level_dir = casepath
)
for test_suite in discover:
for test_case in test_suite:
testunit.addTest(test_case)
print testunit
return testunit
for parent,dirnames,filenames in os.walk( '.' ):
for filename in filenames:
#print "parent is:" + parent
#print "filename is:" + filename
path = os.path.join(parent,filename)
#正则判断是否为测试用例
match = re.match( 'case' , filename)
if match:
print u "获取测试用例目录:%s" % parent
casepaths.append(parent)
break
#定义报告存放目录,支持相对路径
now = time.strftime( "%Y-%m-%M-%H-%M-%S" ,time.localtime(time.time()))
filename = now + 'report.html'
fp = file (filename, 'wb' )
runner = HTMLTestRunner.HTMLTestRunner(
stream = fp,
title = u '自动化测试报告' ,
description = u '用例执行情况'
)
for casepath in casepaths:
print u "正在执行 %s目录下的测试用例" % casepath
alltestnames = createsuite(casepath)
runner.run(alltestnames)
print u "执行 %s目录下的测试用例完成" % casepath
print u "完成所有测试用例执行任务"
|
以上这篇python selenium执行所有测试用例并生成报告的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq1124794084/article/details/53323777