unittest多线程生成报告HTMLTestRunner和(BeautifulReport)

安装tomorrow库 pip install tomorrow
使用多线程tomorrow库,在要创建线程执行的函数上加个@threads(4),括号里面代码线程的数量,数字越大,运行的速度越快

python3下tomorow模块报语法错误def async(n, base_type, timeout=None): ^ SyntaxError: invalid syntax

async是python关键字,换个函数名,比如async_tm

# coding=utf-8

"""

#下面是HTMLTestRunner作用于整个项目的方法
#unittest多线程执行用例
#测试报告的内容错乱,尝试了几种方法还是如此(原因是HTMLTestRunner 获取当前标准输入输出作为case的output,
而多线程时候print本来就不是有序的。)
"""

import unittest
import time
from HTMLTestRunner import HTMLTestRunner
from framework_Var_A.var_A import *
import os
from tomorrow import threads


def add_case():
    '''加载所有的测试用例'''
    test_dir = testcase_path
    # 指定测试用例所在的目录

    # 查找测试用例目录里的用例,赋值给discover
    discover = unittest.defaultTestLoader.discover(testcase_path, pattern='test_*.py')
    print(discover,type(discover))
    return discover
#使用多线程tomorrow库,在要创建线程执行的函数上加个@threads(4),括号里面代码线程的数量,数字越大,运行的速度越快

@threads(4)
def run_case(all_cases):
    t1 = time.time()
    print('start:%s' % time.ctime())
    # 获取当前时间并格式化
    now = time.strftime('%Y-%m-%d %H-%M-%S')
    # 指定测试报告的存放路径,文件名中加了当前时间,以便每次生成不同的报告
    reportfile = os.path.join(report_path, now + 'result.html')

    # 打开报告
    fp = open(reportfile, "wb")
    # 定义报告存放路径
    runner = HTMLTestRunner(stream=fp, title=u"多线程测试报告", description=u"用例执行情况:")

    runner.run(all_cases)
    # 执行测试用例
    fp.close()
    # 关闭报告文件,不关的话生成的报告是空的
    t2=time.time()
    print('end:%s' % time.ctime())
    print('总共花费时间:%s' % (t2-t1))

if __name__ == "__main__":
    cases = add_case()
    #run_case(cases)
    # 之前是批量执行,这里改成for循环执行
    for i in cases:
        print(i,type(i))
        run_case(i)  # 执行用例,生成报告


unittest多线程生成报告(BeautifulReport)
# coding=utf-8
"""
unittest多线程生成报告(BeautifulReport)

"""

import unittest
from BeautifulReport import BeautifulReport
from framework_Var_A.var_A import *
from tomorrow import threads
import time

# 获取测试用例文件所在路径

casepath = testcase_path

def add_case(case_path=casepath, rule="test*.py"):
    '''加载所有的测试用例'''
    discover = unittest.defaultTestLoader.discover(case_path,
                                                   pattern=rule,
                                                   top_level_dir=None)
    return discover


@threads(3)
def run(test_suit):
    # now = time.strftime("%Y-%m-%d %H%M%S", time.localtime(time.time()))

    # 获取当前时间并格式化
    now = time.strftime('%Y-%m-%d %H-%M-%S')
    # 文件名中加了当前时间,以便每次生成不同的报告
    Beautifulfile = os.path.join(now + 'Beautiful.html')

    # BeautifulReport(suite_tests)执行测试用例生成报告,并定义报告属性
    BeautifulReport(test_suit).report(filename=Beautifulfile, description='登录测试', log_path=report_path)

    # result = BeautifulReport(test_suit)
    # result.report(filename='report.html', description='测试deafult报告', log_path='report')


if __name__ == "__main__":
    # 用例集合
    cases = add_case()

    print(cases)
    for i in cases:
        print(i)
        run(i)

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值