ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
>[info] 本章节,将在测试代码中结合Allure,输出美观的测试报告 > [TOC] ## Allure测试报告介绍 Allure是一款非常轻量级并且非常灵活的开源测试报告生成框架。 它支持绝大多数测试框架, 例如TestNG、Pytest、JUint等 官网地址:[https://docs.qameta.io/allure/](https://docs.qameta.io/allure/) ## Pytest框架集成Allure ### 安装 allure-pytest 通过allure-pytest,我们可以**生成Allure2所需要的用于生成测试报告的数据**。 ```cmd pip install allure-pytest ``` ### 改造Pytest的测试用例 为了使用Allure生成报告,需要在conftest.py和测试脚本中加入Allure特性。 下面直接用前面的项目为例,直接介绍如何将Allure应用到自己的测试项目中。 * 首先将环境信息,放到公共fixture中,改造后 `conftest.py`如下: ```python from selenium import webdriver import pytest @pytest.fixture() def get_driver(request): # 创建Chrome驱动实例,这里创建driver时,传入chrome_options参数,告诉服务器,我是用移动端浏览器访问的。 options = webdriver.ChromeOptions() options.add_argument('User-Agent=Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus Build/ICL53F) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30') driver = webdriver.Chrome(chrome_options=options) # 设置浏览器大小,让它看起来跟手机的样式差不多。 driver.set_window_size("380", "680") # 设置一个全局的等待超时时间 10s driver.implicitly_wait(10) def fin(): driver.quit() request.addfinalizer(fin) return driver ``` * 改造测试代码,加入Allture特性,改造后的 `test_login.py`如下: ```python import pytest from page import LoginPage import allure @allure.feature('登录模块') class TestLogin(object): @allure.story('正确登录') def test_login_succss(self,get_driver): """登录成功用例""" with pytest.allure.step('初始化浏览器driver'): login_page = LoginPage(get_driver) with pytest.allure.step('输入正确的用户名与密码'): login_page.login("gfc@qq.com", "123456") with pytest.allure.step('登录后,页面源码中包含用户名信息'): assert "gfc@qq.com" in login_page.get_source() @allure.story('错误登录') def test_login_fail(self,get_driver): """登录失败用例""" with pytest.allure.step('初始化浏览器driver'): login_page = LoginPage(get_driver) with pytest.allure.step('使用错误的账号与密码登录'): login_page.login("gfc@123.com", "123456") alert = login_page.accept_alert() with pytest.allure.step('弹窗提示信息包含:User does not exist!'): assert "User does not exist!" in alert.text if __name__ == '__main__': import os pytest.main(['-s', '-q', 'test_login.py','--alluredir', './result/']) os.system('allure serve ./result/') ``` 上面使用了Allure的几个特性: * @allure.feature # 用于定义被测试的功能,被测产品的需求点 * @allure.story # 用于定义被测功能的用户场景,即子功能点 * with allure.step # 用于将一个测试用例,分成几个步骤在报告中输出 * allure.attach # 用于向测试报告中输入一些附加的信息,通常是一些测试数据信息 * @pytest.allure.step # 用于将一些通用的函数作为测试步骤输出到报告,调用此函数的地方会向报告中输出步骤 ### 生成Allure测试报告的数据 在测试脚本中添加了Allure特性之后,pytest 执行测试的时候,指定–-alluredir选项及测试数据保存的目录,即可在指定目录下生成Allure测试报告的数据 >[warning] 在生成allure报告前,我们需要先运行测试用例,生成基础数据。 在本示例中,运行以下命令 ```cmd pytest test_login.py --alluredir=./result/ ``` 运行后,我们可以看到在result目录下,生成了两个xml,显然,这还不是我们预期的美观html报告,这只是测试报告的数据而已。 ## Allure测试报告 ### 安装 allure-commandline 要生成Allure测试报告,需要使用到allure命令行工具,下载地址: [https://github.com/allure-framework/allure2](https://github.com/allure-framework/allure2) https://pan.baidu.com/s/1LoPJQM27PfGwXWZ--bs8XQ 下载后,记得要配置到环境变量中,例如将其解压到本地目录D:\360YP\VM\allure-2.7.0\bin\allure.bat 后,要将该路径添加到电脑环境变量的path中。 > 提示:要运行该命令行工具,需要提前安装好java运行环境。 > ### 生成并打开Allure测试报告 方式一 ```cmd allure serve ./result/ ``` 方式二 ```cmd allure generate ./result/ -o ./report/ --clean allure open -h 127.0.0.1 -p 8083 ./report/ ``` 运行后本机的浏览器将打开 `http://127.0.0.1:8083/index.html` 网页,展示测试报告,测试报告如下: ![](https://box.kancloud.cn/0361d0d64310b562c19514c7ca51a25f_1228x769.jpg) ![](https://box.kancloud.cn/161b417bfbb1b43c7a621cc9998ae1fc_1305x547.jpg) ## 参考文档 [https://docs.qameta.io/allure/latest/](https://docs.qameta.io/allure/latest/) <hr style="margin-top:50px"> <section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="margin: 40px 0% 10px;box-sizing: border-box;"><section class="" style="display: inline-block;width: 100%;border-width: 5px;border-style: double;border-color: rgb(23, 22, 24);padding: 10px;border-radius: 2px;box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="transform: translate3d(20px, 0px, 0px);-webkit-transform: translate3d(20px, 0px, 0px);-moz-transform: translate3d(20px, 0px, 0px);-o-transform: translate3d(20px, 0px, 0px);font-size: 11px;margin: -50px 0% 0px;box-sizing: border-box;"><section class="" style="box-sizing: border-box;width: 7em;height: 7em;display: inline-block;vertical-align: bottom;border-radius: 100%;border-width: 4px;border-style: double;border-color: rgb(23, 22, 24);background-position: center center;background-repeat: no-repeat;background-size: cover;background-image: url(&quot;http://pav7h2emv.bkt.clouddn.com/FnD-fHkNDLN1-b02XmnMvsz6ld-n&quot;);"><section class="" style="width: 100%;height: 100%;overflow: hidden;box-sizing: border-box;"><img class="" data-ratio="0.6012024" data-w="499" data-src="http://pav7h2emv.bkt.clouddn.com/FnD-fHkNDLN1-b02XmnMvsz6ld-n" style="opacity: 0; box-sizing: border-box; width: 100% !important; height: auto !important; visibility: visible !important;" width="100%" data-type="jpeg" _width="100%" src="http://pav7h2emv.bkt.clouddn.com/FnD-fHkNDLN1-b02XmnMvsz6ld-n" data-fail="0"></section></section></section></section><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="margin: -30px 0% 30px;box-sizing: border-box;"><section class="" style="display: inline-block;vertical-align: top;width: 61.8%;padding: 0px 15px;box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="margin: 40px 0% 0px;box-sizing: border-box;"><section class="" style="color: rgb(160, 160, 160);box-sizing: border-box;"><p style="margin: 0px;padding: 0px;box-sizing: border-box;">微信公众号:</p><p style="margin: 0px;padding: 0px;box-sizing: border-box;">python测试开发圈</p><p style="margin: 0px;padding: 0px;box-sizing: border-box;"><br style="box-sizing: border-box;"></p></section></section></section></section><section class="" style="display: inline-block;vertical-align: top;width: 38.2%;box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="text-align: center;margin: 10px 0% 0px;box-sizing: border-box;"><section class="" style="max-width: 100%;vertical-align: middle;display: inline-block;border-width: 0px;border-radius: 0px;box-shadow: rgb(0, 0, 0) 0px 0px 0px;width: 90%;overflow: hidden !important;box-sizing: border-box;"><img data-ratio="1" data-w="430" data-src="http://pav7h2emv.bkt.clouddn.com/FibGgIJSMfHtehzeWOOzjdQKSMx5" style="vertical-align: middle; max-width: 100%; box-sizing: border-box; width: 100% !important; height: auto !important; visibility: visible !important;" width="100%" data-type="jpeg" _width="100%" class="" src="http://pav7h2emv.bkt.clouddn.com/FibGgIJSMfHtehzeWOOzjdQKSMx5" data-fail="0"></section></section></section></section></section></section><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="margin: -30px 0% 0px;box-sizing: border-box;"><section class="" style="display: inline-block;vertical-align: top;width: 61.8%;padding: 0px 15px;box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="transform: translate3d(5px, 0px, 0px);-webkit-transform: translate3d(5px, 0px, 0px);-moz-transform: translate3d(5px, 0px, 0px);-o-transform: translate3d(5px, 0px, 0px);box-sizing: border-box;"><section class="" style="color: rgb(160, 160, 160);font-size: 14px;box-sizing: border-box;"><p style="margin: 0px;padding: 0px;box-sizing: border-box;">一起分享学习与成长路线</p></section></section></section></section><section class="" style="display: inline-block;vertical-align: top;width: 38.2%;box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="transform: translate3d(10px, 0px, 0px);-webkit-transform: translate3d(10px, 0px, 0px);-moz-transform: translate3d(10px, 0px, 0px);-o-transform: translate3d(10px, 0px, 0px);box-sizing: border-box;"><section class="" style="color: rgb(160, 160, 160);font-size: 14px;box-sizing: border-box;"><p style="margin: 0px;padding: 0px;box-sizing: border-box;">长按(或扫描)二维码关注</p></section></section></section></section></section></section></section></section></section>