背景
allure可以生成更清晰的测试报告
1.安装
下载allure-command-line
- 如何下载?github.com/AllureOfThe… 右下角点击realse
配置环境变量
1. 复制安装路径 D:\xxx\allure-commandline-2.19.0\allure-2.19.0\bin
2. 电脑底部搜path-环境变量-系统变量-path-新建-粘贴路径-确定
3. 命令行验证 输入 allure
安装allure-pytest库
pip install allure-pytest
2.如何生成测试报告?
命令行形式
pytest --alluredir=output output表示测试报告生成在output的文件夹下
python中如何生成?
import pytest
if __name__ == '__main__':
# 运行test文件夹,test_login文件
pytest.main(['tests\\test_login.py', '--alluredir=output'])
如何查看测试报告?
手动启动allure服务 allure serve output output表示测试报告的地址
3.allure报告应用
3.1allure报告添加截屏
import allure
f = driver.get_screenshot_as_png()# 一张图片的二进制数据
allure.attach(f,name="登录截屏",attachment_type=allure.attachment_type.PNG)
attach方法需要的参数:
文件的二进制数据 name 截屏名称 attachment_type 选择不同的格式
- 截图可以封装在basepage中,供自动化用例使用
# basepage.py封装
class BasePage:
def __init__(self, driver):
self.driver = driver
def allure_screenshot(self,name=None):
"""截图"""
f =self.driver.get_screenshot_as_png()
return allure.attach(f,name=name,attachment_type=allure.attachment_type.PNG)
#testcase调用
from common.basepage import BasePage
class LoginPage(BasePage):
...
def test_login_error_username(self, case_info, driver):
...
login_page = LoginPage(driver)
login_page.allure_screenshot('登录截图')
思路:可以在异常处理加截图
示例:
def drag_and_drop(self, locator_start, locator_end):
"""拖拽"""
try:
start_el = self.driver.find_element(*locator_start)
end_el = self.driver.find_element(*locator_end)
action = ActionChains(self.driver)
action.drag_and_drop(start_el, end_el).perform()
except:
log.error("拖拽操作报错")
self.allure_screenshot('拖拽报错截屏')
3.2allure step测试步骤标记
@allure.step(“步骤名称”) 在需要的方法上面加
3.3 显示测试用例的标题、测试套件的标题
@allure.suit("测试套件") 在需要的方法、类上面加
@allure.title("测试标题")在需要的方法上面加
4.allure和jenkins一起使用
前置:用gitee本地代码推远程服务器 流程:(不用服务器域运行自动化代码,用本地电脑运行)
1.配置节点
- 新建节点a(点击Dash>系统管理>节点管理>新建节点)
- 配置节点a,配置远程工作目录,启动方式选择通过 java web启动代理,工具位置:
- git必须配置到文件 xxx\git.exe
- allure必须配置到目录 xxx\allure-2.14.0
- 连接节点a
2.新建任务
3.jenkins任务配置
-
指定节点a
- 点击进任务-General 勾选限制项目的运行节点 -标签表达为节点名称a
-
源码管理-Git-输入url-用户名密码
-
构建触发器,输入命令
pip install -r requirement.txt
pytest --alluredir=output
3.构建后操作
-
先allure插件下载(系统管理-插件管理-可选插件-输入allure) Allure Jenkins Plugin
-
构建后的操作-选择Allure Report-改一下path(测试报告预期放入的文件夹)
4、点击构建,查看效果