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

yipeiwu_com5年前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设计】。

相关文章

利用Python yagmail三行代码实现发送邮件

利用Python yagmail三行代码实现发送邮件

Python 发送邮件 我以前在通过Python实现自动化邮件功能的时候是这样的: import smtplib from email.mime.text import MIMET...

Python用户推荐系统曼哈顿算法实现完整代码

Python用户推荐系统曼哈顿算法实现完整代码

出租车几何或曼哈顿距离(Manhattan Distance)是由十九世纪的赫尔曼·闵可夫斯基所创词汇 ,是种使用在几何度量空间的几何学用语,用以标明两个点在标准坐标系上的绝对轴距总和。...

python leetcode 字符串相乘实例详解

给定两个以字符串形式表示的非负整数 num1 和  num2 ,返回  num1 和  num2 的乘积,它们的乘积也表示为字符串形式。 示例 1: 输入:...

python快速编写单行注释多行注释的方法

python快速编写单行注释多行注释的方法

在python代码编写过程中,养成注释的习惯非常有用,可以让自己或别人后续在阅读代码时,轻松理解代码的含义。 如果只是简单的单行注释,可直接用“#”号开头,放于代码前面。 单行注释也可...

用Python写冒泡排序代码

python代码实现冒泡排序代码其实很简单,具体代码如下所示: 代码Code highlighting produced by Actipro CodeHighlighter (fr...