python selenium执行所有测试用例并生成报告的方法

yipeiwu_com6年前Python基础

直接上代码。

# -*- 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执行所有测试用例并生成报告的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

django 在原有表格添加或删除字段的实例

一、如果models.py文件为时: timestamp = models.DateTimeField('保存日期') 会提示: (env8) D:\Desktop\env8\...

python 实现方阵的对角线遍历示例

任务描述 对一个方阵矩阵,实现平行于主对角线方向的对角线元素遍历。 从矩阵索引入手: [[ 1 2 3 4 5] [ 6 7 8 9 10] [11 12 13 14 15]...

对python中raw_input()和input()的用法详解

最近用到raw_input()和input()来实现即时输入,就顺便找了些资料来看,加上自己所用到的一些内容,整理如下: 1、raw_input() raw_input([promp...

Python小程序 控制鼠标循环点击代码实例

这篇文章主要介绍了Python小程序 控制鼠标循环点击代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 from ctype...

python3中替换python2中cmp函数的实现

python3中替换python2中cmp函数的实现

python 3.4.3 的版本中已经没有cmp函数,被operator模块代替,在交互模式下使用时,需要导入模块。 在没有导入模块情况下,会出现 提示找不到cmp函数了,那么在p...