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

相关文章

解决python3中自定义wsgi函数,make_server函数报错的问题

#coding:utf-8 from wsgiref.simple_server import make_server def RunServer(environ, start_...

解决django model修改添加字段报错的问题

解决django model修改添加字段报错的问题

关于django models中添加字段的一个小节,记录下 django的models中已经写好了字段,可是后面我又想在添加一些字段,于是就在models中添加了字段 ,发现报错了 报...

对python添加模块路径的三种方法总结

之前对mac os系统自带的python进行了升级,结果发现新安装的python的site-packages目录并没有加到python的系统路径中,所以在使用其他库时发现出现了缺少模块的...

解决python中os.listdir()函数读取文件夹下文件的乱序和排序问题

解决python中os.listdir()函数读取文件夹下文件的乱序和排序问题

1. os.listdir()概述 os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表。 例如: dir ='F:/Home_01/img'#当前目录...

python在Windows下安装setuptools(easy_install工具)步骤详解

python在Windows下安装setuptools(easy_install工具)步骤详解

本文讲述了python在Windows下安装setuptools(easy_install工具)的方法。分享给大家供大家参考,具体如下: 【题外话介绍下setuptools】 setup...