搜索
您的当前位置:首页正文

使用python生成测试报告

来源:二三娱乐

这里使用的是BSTestRunner库。

BSTestRunner简介

BSTestRunner是HTMLTestRunner的bootstrap3版本(现在支持python2和python3)用于生成 HTML的测试报告,需要将文件放在…\python\Lib目录下 ,使用时需先import

生成测试报告

  • 首先创建文件夹reports 用来存放测试报告
  • 创建run.py模块

run.py

import unittest
from BSTestRunner import BSTestRunner
import time

# 用例所在路径
test_dir = ./test_case

# 加载测试用例
discover = unittest.defaultTestLoader.discover(test_dir,pattern="test*.py")

if __name__ == ‘__main__‘:

    # 测试报告存放路径
    report_dir = './test_report'

    # 定义报告的文命名方式
    now = time.strftime("%Y-%m-%d %H_%M_%S")
    report_name = report_dir + '/' + now + 'result.html'

   # 运行用例并生成报告
    with open(report_name,‘wb‘)as f:
        runer = BSTestRunner(stream=f,title="Test Report",description="Test case result")
        runer.run(discover)

运行之后生成报告如下:

测试报告
Top