您的位置:首页技术文章
文章详情页

allure结合python生成测试报告教程

浏览:50日期:2022-06-17 15:52:18

百度搜索实例

一、代码结构

本案例来自于霍格沃兹测试学院《高薪测试成长图谱》。data.yml为数据管理文件,test_baidudemo.py为测试用例文件,文件结构如下:

allure结合python生成测试报告教程

创建data/data.yml文件,代码如下

- allure- pytest- unittest

创建test_baidudemo.py,代码如下

#!/usr/bin/env python# -*- coding: utf-8 -*-from __future__ import unicode_literalsimport allureimport pytestimport yamlfrom selenium import webdriverimport time@allure.testcase('https://www.baidu.com')@allure.feature('百度搜索')@pytest.mark.parametrize(’test_data1’,yaml.safe_load(open('data/data.yml')))def test_steps_demo(test_data1): with allure.step('打开百度网页'):driver = webdriver.Chrome()driver.get('https://www.baidu.com')driver.maximize_window() with allure.step(f'输入搜索词:{test_data1}'):driver.find_element_by_id('kw').send_keys(test_data1)time.sleep(2)driver.find_element_by_id('su').click()time.sleep(2) with allure.step('保存图片'):driver.save_screenshot('./result/b.png')allure.attach.file('./result/b.png',attachment_type=allure.attachment_type.PNG) with allure.step('关闭浏览器'):driver.quit()二、运行结果

进入项目目录下,使用以下语句运行

pytest test_baidudemo.py -s -q --alluredir=./result/ #执行测试用例,并生成测试报告数据在当前文件夹result文件下allure serve ./result/ #启动allure,并使用result下的测试结果数据生成测试报告

生成的报告如下图所示:

allure结合python生成测试报告教程

allure结合python生成测试报告教程

问题解决

运行时总是报错当前chromedriver只支持chrome78,实际上已经更新了chromedriver83,未找到原因解决,最终在代码里加上chromedriver绝对路径。将driver = webdriver.Chrome()修改为driver = webdriver.Chrome(‘C:Program Files (x86)GoogleChromeApplicationchromedriver.exe’)。

with allure.step(f'输入搜索词:{test_data1}'):,在python3.6.8版本上运行该语句总是报语法错误,修改为 with allure.step(“输入搜索词:”+test_data1):,可以正常运行并输出。

allure简介与使用allure简介

Allure是一款轻量级并且非常灵活的开源测试报告框架。 它支持绝大多数测试框架, 例如TestNG、Pytest、JUint等。它简单易用,易于集成。

allure如何生成测试报告

运行的时候加上 pytest.main ( ‘?alluredir’, ‘report/result’, ‘TestDemo01.py’]) 会在当前文件夹创建一个report文件夹,在report文件夹下创建result

allure结合python生成测试报告教程

allure结合python生成测试报告教程

生成html测试报告

因为生成的测试报告是json的,不好看,所有用这个命令生成一个好看的HTML测试报告

allure结合python生成测试报告教程

运行之后,就会生成一个HTML文件夹,点开index.html这个就是我们的测试报告啦

allure结合python生成测试报告教程

allure结合python生成测试报告教程

allure几个常用特性(测试报告中展示)

@allure.feature (用于描述被测试产品需求)

allure结合python生成测试报告教程

@allure.story (用于描述feature的用户场景,即测试需求)

allure结合python生成测试报告教程

with allure.step() (用于描述测试步骤,将会输出到报告中)

allure结合python生成测试报告教程

allure.attach (用于向测试报告中输入一些附加的信息,通常是一些测试数据,截图等)

allure结合python生成测试报告教程

pytest断言设置并结合allure生成测试报告

allure结合python生成测试报告教程

测试报告

allure结合python生成测试报告教程

以上为个人经验,希望能给大家一个参考,也希望大家多多支持好吧啦网。

标签: Python 编程
相关文章: