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

相关文章

对python:print打印时加u的含义详解

u:表示unicode字符串 不是仅仅是针对中文, 可以针对任何的字符串,代表是对字符串进行unicode编码。 一般英文字符在使用各种编码下, 基本都可以正常解析, 所以一般不带u;...

Python操作MySQL数据库9个实用实例

Python操作MySQL数据库9个实用实例

在Windows平台上安装mysql模块用于Python开发 用python连接mysql的时候,需要用的安装版本,源码版本容易有错误提示。下边是打包了32与64版本。 MySQL-py...

Python判断以什么结尾以什么开头的实例

如下所示: str='abcdef' print(str.endswith('f')) print(str.startswith('a')) 输出结果: True True...

利用Python暴力破解zip文件口令的方法详解

利用Python暴力破解zip文件口令的方法详解

前言 通过Python内置的zipfile模块实现对zip文件的解压,加点料完成口令破解 zipfile模块用来做zip格式编码的压缩和解压缩的,zipfile里有两个非常重要的cla...

numpy:找到指定元素的索引示例

目的:在numpy数组中知道指定元素的索引 函数: np.argwhere >>>x >>>array([[0, 1, 2], [3, 4,...