干货分享丨通过python生成4种不同的测试报告,过程对比-凯发体育客户端

空空 2023-03-20 06:51:49 生活百科 421 ℃ 0 评论

无论做什么自动化,测试报告一定要有的,它可以清楚的展示出来我们执行用例的情况。便于查看自动化测试结果内容。安静这边了解目前通过python生成的测试报告分别有:htmltestrunner、beautifulreport 、pytest-html和allure,这几种报告内容都是属于不同的模板,本篇文章主要介绍下这如何生成以上四份报告的过程以及对比情况。

htmltestrunner

htmltestrunner是python标准库的unittest模块的扩展。它生成易于使用的html测试报告。使用时需要下载,然后放到项目目录中

下载完成后,我们打开文件,其中有一段代码是告诉我们如何进行使用。

# output to a file

fp = file('my_report.html', 'wb')

runner = htmltestrunner.htmltestrunner(

stream=fp,

title='my unit test',

description='this demonstrates the report output by htmltestrunner.'

)

# use an external stylesheet.

# see the template_mixin class for more customizable options

runner.stylesheet_tmpl = ''

# run the test

runner.run(my_test_suite)

从上面代码中可以看出来,报告需要三个参数:

·stream:表示生成报告的路径

·title:表示报告的标题

·description:表示用例执行情况说明

安静来一段实例代码,来生成详细的测试报告:

import unittest

import htmltestrunner_cn

class test(unittest.testcase):

def test_01(self):

'''测试用例01'''

print('---用例01---')

def test_02(self):

print('---用例02---')

def test_03(self):

'''测试用例03'''

print('---用例03---')

if __name__ == '__main__':

# 测试报告地址

fp = open('result.html', "wb")

# 报告详情

runner = htmltestrunner_cn.htmltestrunner(stream=fp,

title=u'自动化测试报告,测试结果如下:',

description=u'用例执行情况:')

# 实例化

testunit = unittest.testsuite()

# 加载用例

testunit.addtests(unittest.testloader().loadtestsfromtestcase(test))

# 执行用例

runner.run(testunit)

# 关闭报告

fp.close()

执行完脚本后,本地路径出现了一个测试报告,双击打开可以查看报告详情,其中用例中加入注释,可以方便配置用例详情显示。

beautifulreport

beautifulreport也是基于unittest中的一个报告框架,也是以html的报告形式进行展示出来的,可以通过pip直接进行安装下载。

安装:pip install beautifulreport

验证安装:pip show beautifulreport

打开beautifulreport的源码可以看到报告中已经将需要的参数列举下来了。

以上参数可以默认也可以进行自己手动修改。

import unittest

import beautifulreport

class test(unittest.testcase):

def test_01(self):

'''测试用例01'''

print('---用例01---')

def test_02(self):

print('---用例02---')

def test_03(self):

'''测试用例03'''

print('---用例03---')

if __name__ == '__main__':

testunit = unittest.testsuite()

# 加载用例

testunit.addtests(unittest.testloader().loadtestsfromtestcase(test))

result = beautifulreport.beautifulreport(testunit)

result.report(filename='report', description='测试报告', log_path=none)

通过执行后可以看出生成了和htmltestrunner类似的测试报告。

pytest-html

pytest-html属于pytest第三方插件,使用时需要安装pytest和pytest-html。

安装pytest:pip install pytest-html

安装pytest-html:pip installpytest-html

pytest-html的源码地址:https://github.com/pytest-dev/pytest-html

使用方法:

需要在执行的用例下通过终端形式输入命令 pytest --html=report.html (等号后面表示生成报告的路径和名称)。

class test:

def test_01(self):

'''测试用例01'''

print('---用例01---')

def test_02(self):

print('---用例02---')

def test_03(self):

'''测试用例03'''

print('---用例03---')

通过执行后,会在当前目录中生成一个report.html。

双击进行打开报告,可以看出详情的测试报告内容。可以看到和htmltestrunner的报告类似。

allure

allure也是属于一种开源的测试报告框架,基于多种语言,其中在python中是通过pytest单元测试框架进行生成的。也是目前测试行业中常用到的测试报告内容。其中allure是的环境是基于java的环境(下载jdk,配置环境变量)。

allure下载地址:https://github.com/allure-framework/allure2

下载后通过解压,将allure中的bin文件放入到环境变量中即刻进行使用

我们是通过pytest单元测试框架进行生成allure报告的,需要安装第三方插件 allure-pytest,可以通过pip直接进行安装 pip install allure-pytest。

class test_allure:

def test_01(self):

'''测试用例01'''

print('---用例01---')

def test_02(self):

print('---用例02---')

def test_03(self):

'''测试用例03'''

print('---用例03---')

想要生成测试报告需要先在执行测试用例时候,在命令行中加入生成报告的命令。

pytest --alluredir 报告目录

执行完后,会在当前目录中生成一个详细的report的报告目录,里面存放的就是执行用例的测试结果内容。

再次输入命令:

allure serve 测试结果目录

通过执行命令后,会在当前的浏览器中直接打开我们的测试报告内容。

总结

通过本篇文章,安静已经将目前市面上常用到的测试框架进行了总结,我想通过大家对图片中的报告认知,可能会感觉,htmltestrunner和beautifulreport,pytest-html生成的报告内容基本差不多,将测试结果可以在一个页面中全部展示出来,但是我们的allure报告,存在多个页面内容,如果测试用例较多的话,可以进行查看更加方便快捷。好了,具体用到那个报告,就看个人爱好了~感谢大家阅读,希望本篇文章对您有所帮助!

推荐阅读:

同类热门推荐 一文讲透!实现一个python selenium的自动化测试框架如此简单! 99409人看过 unittest扩展之html 测试报告(上) 39837人看过 澳洲更新对测试报告中的关键元器件的要求 17912人看过 selenium python怎么搭建自动化测试框架、执行自动化测试用例、生成自动化测试报告、发送测试报告邮件 8734人看过 python selenium自动化测试框架详解,我只讲一遍 94066人看过 本站只为传播信息,不对所发布的内容本身负责。如有凯发k8国际手机app下载的版权及其它问题,请联系站长处理。

本文tag:[分享] [通过] [不同]

  • 上一篇:
  • 下一篇:
网站地图