1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 自动化测试框架Pytest介绍(8)——利用allure生成测试报告

自动化测试框架Pytest介绍(8)——利用allure生成测试报告

时间:2024-02-28 21:18:41

相关推荐

自动化测试框架Pytest介绍(8)——利用allure生成测试报告

目录

1、安装allure

1.1安装allure

1.2安装allure-pytest

2、allure测试报告生成

2.1步骤说明

2.2代码示意

3、测试报告优化

3.1allure报告中文显示

3.2 对 几个常用优化方法的代码示例

上一篇我们介绍了利用pytest-html生成测试报告,本篇我们介绍用allure生成测试报告,allure跟pytest-html相比,无论美观性、可用性都更强一些。

1、安装allure

1.1安装allure

1、在github上搜索allure,进入allure-framework/allure2, 拉倒下面的Download,点击releases

2、进入后选择下载包,我们是Windows系统,下载zip包

3、将下载的zip包解压到自己想存放allure的目录,并将解压后的bin目录加入环境变量

4、打开命令行,运行 allure --version,可以看到版本号,证明安装成功

1.2安装allure-pytest

pytest要生成allure报告,还需要安装allure-pytest,直接用pip安装即可,没办法联网的则到pypi下载allure-pytest包,然后离线安装。

安装命令

pip install allure-pytest

2、allure测试报告生成

2.1步骤说明

我们还是用pytest结合allure来生成测试报告,pytest执行完后生成allure测试报告分3个步骤

1、执行用例时用--alluredir 参数,生成原始json文件

2、用allure generate ,将生成的json文件生成html文件

3、用allure open 打开报告(特别是这条容易遗忘,如果直接打开生成的index.html文件,会发现显示Loading,并且没有任何跟测试内容相关的东西)

2.2代码示意

from BaseLog import loggerimport pytestimport datetimeimport timeimport osdef test_case1():"""测试用例1"""Expected = 2Actual = 2time.sleep(1)print("我是test_case1:")assert Expected == Actual,"Expected == Actual"def test_case2():"""测试用例2"""Expected = 1Actual = 2print("我是test_case2")assert Expected != Actual,"Expected == Actual"def test_case3():"""测试用例3"""Expected = 1Actual = 2print("我是test_case3")assert Expected == Actual,"Expected == Actual"if __name__ =="__main__":# -s:显示用例中的输出# -v:输出更详细的用例执行信息# __file__:本文件# 生成YYYY-mm-dd-HHMMSS的时间戳now = datetime.datetime.now()time_str = now.strftime("%Y-%m-%d-%H%M%S") #此处命令 --alluredir 生成了报告的原始文件pytest.main(["-vs","BASE/file_test.py","--alluredir","./Report/{}xml".format(time_str)])#此处命令 allure generate 将前面生成的json文件转换为html的报告os.system("allure generate ./Report/{}xml -o ./Report/{}html --clean".format(time_str,time_str))# 生成的报告index.html不能直接用Chrome打开,打开后看不到内容,需要用allure open打开才能渲染出样式和显示内容os.system("allure open ./Report/{}html".format(time_str))

会生成如下2个文件煎

执行完后浏览器打开测试报告,长下面的样子

3、测试报告优化

上面的报告有几个问题,首先是英文显示,另外很选项都没有内容,我们逐个设置

3.1allure报告中文显示

这个比较简单,在报告的左下角点进En就可以切换语言了

3.2 优化项说明

主要可以优化的地方见下表

3.2 对 几个常用优化方法的代码示例

file_test.py中的代码

'''FilePath: \Android_Test\BASE\file_test.py'''from BaseLog import loggerimport pytestimport datetimeimport timeimport osimport allure@allure.epic("针对allure这个工具的测试说明")@allure.feature("这是测试用例类1")class TestClass1:@allure.story("验证用的测试用例1")def test_case1(self):"""测试用例1"""Expected = 2Actual = 2time.sleep(1)print("我是test_case1:")assert Expected == Actual,"Expected == Actual"@allure.story("验证用的测试用例2")def test_case2():"""测试用例2"""Expected = 1Actual = 2print("我是test_case2")assert Expected == Actual,"Expected == Actual"#注意这个模块里面用到了两个allure.epic,并且文本内容相同,这样所有的feature才会处于相同的epic之下@allure.epic("针对allure这个工具的测试说明")@allure.feature("这是是测试用例类TestClass2")class TestClass2:@allure.story("在TestClass里面验证用的测试用例3")@allure.severity(allure.severity_level.BLOCKER)def test_case3(self):"""测试用例3"""with allure.step("测试步骤一、获取预期值和实际值"):Expected = 1Actual = 2print("我是test_case3")with allure.step("测试步骤一、对预期值和实际值进行断言"):assert Expected == Actual,"Expected == Actual"@allure.story("在TestClass里面验证用的测试用例4")@allure.severity(allure.severity_level.CRITICAL)def test_case4(self):"""测试用例3"""with allure.step("测试步骤一、获取预期值和实际值"):Expected = 1Actual = 2#with allure.stepprint("我是test_case4")with allure.step("测试步骤二、截图"):# 这个截图一般是执行步骤失败的时候才进行的allure.attach.file("./screenshot/0304082241.png","截图",attachment_type = allure.attachment_type.PNG)with allure.step("测试步骤三、预期结果断言"):#测试用例中的断言不能用try捕获异常,否则用例会执行成功assert Expected == Actual,"Expected != Actual" if __name__ =="__main__":# -s:显示用例中的输出# -v:输出更详细的用例执行信息# __file__:本文件# 生抽YYYY-mm-dd-HHMMSS的时间戳now = datetime.datetime.now()time_str = now.strftime("%Y-%m-%d-%H%M%S") #此处命令 --alluredir 生成了报告的原始文件pytest.main(["-vs","BASE","--alluredir","./Report/{}xml".format(time_str)]) #此处命令 allure generate 将起那么生成的json文件转换为html的报告os.system("allure generate ./Report/{}xml -o ./Report/{}html --clean".format(time_str,time_str))# 生成的报告index.html不能直接用Chrome打开,打开后看不到内容,需要用allure open打开才能渲染出样式和显示内容os.system("allure open ./Report/{}html".format(time_str))

另外一个测试模块文件中的代码

'''LastEditTime: -03-06 23:25:51FilePath: \Android_Test\BASE\test_log2.py'''from BaseLog import loggerimport pytestimport allure@allure.epic("针对allure这个工具的测试说明在test_log2")def test_caselog():assert 1print("这是在test_log2中的test_caselog")if __name__ =="__main__":# -s:显示用例中的输出# -v:输出更详细的用例执行信息# __file__:本文件pytest.main(["-vs","BASE"])

执行后的测试报告说明

总览页面

我们设置的各种功能名,要在功能标签页才能展现

用例详细情况

如上所述测试报告的使用基本上就用完了,还有一些通过allure的用例级别来跑不通用例的,建议直接用MARK来实现更纯粹,测试报告就纯粹当报告用。

PS:这是pytest系列的最后一篇了,我们经过8篇文章,从pytest的安装、简单使用、参数化、前置和后置、测试报告生成等方面阐述了pytest的使用。虽然都比较浅,但掌握这些已经可以用pytest做自动化测试了。其他更深入到内容在自己使用过程中挖掘就行了。 关键还是要多用

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。