allure测试报告的应用
1.电脑必须安装JDK1.8,下载地址:
https://www.oracle.com/java/technologies/downloads/#java8-windows;我这里下载的是
jdk-8u341的版本
2.安装JDK1.8,并配置环境变量
3.下载Allure的包,官网下载地址为:
https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/
我这里下载的是2.18.1的版本
4.将Allure的包放到电脑上的非中文名称的目录,解压,并配置环境变量
5.检查allure是否配置成功,打开dos窗口,输入:allure --version
显示的版本号为2.18.1表示你配置好了
6.由于我们需要使用pytest集成allure,所以需要下载安装allure-pytest;安装命令:pip install
allure-pytest;检查安装是否成功的命令:pip show allure-pytest
7.注意Pycharm中的需要配置的执行框架为Unittest,否则不会生成测试报告
2.使用allure生产测试报告
import os,pytest
def step_01():
print('点击登录链接')
print('输入用户名密码,点击登录')
def step_02():
print('输入商品名称')
print('点击搜索按钮')
class TestShopping:
def test_01(self):
step_01()
step_02()
print('测试用例:test_01')
def test_02(self):
print('测试用例:test_02')
if __name__ == '__main__':
# __file__:指定运行的测试文件为当前文件
# --alluredir=report/tmp:指定测试结果数据的存储路径为:当前的report目录下的tmp目录
# --clean-alluredir:以免旧数据被加载了,清空测试结果目录
pytest.main([__file__,'--alluredir=report/tmp','--clean-alluredir'])
#利用os.system执行命令行命令:allure generate
# allure generate 测试结果目录 -o 测试报告存放目录 --clean
# --clean:清空测试报告目录,让新的报告工程可以继续使用以前的报告目录
os.system('allure generate report/tmp -o report/html --clean')
#直接生成测试报告,快速生成,与上面的二选一
#os.system('allure serve report/html')
测试报告结果如图:
3、Allure的特性
1.@allure.epic():epic描述,敏捷测试里面的概念,定义为史诗,一般用于描述测试的一级模块名称,修
饰测试文件里面的的类,下面未feature
2.@allure.feature():模块描述,用于定义测试用例的所属模块二级模块,通常修饰测试用例的类,下面
未story
3.@allure.story():定义故事,用于描述测试的业务场景,修饰测试用例方法,下面是title
4.@allure.title():定义测试用例名称,修饰测试用例方法,下面是step
5.@allure.testcase(url,'测试用例管理工具地址'):添加连接,显示测试用例管理工具的地址,便于出
问题核对
6.@allure.issue(url,'bug提单管理工具地址'):添加连接,显示bug提单工具地址,便于出问题提单
7.@allure.severity():定义测试用例的等级,修饰测试用例方法,一共5个等级:
blocker:阻塞,对应门槛用例,重要业务主流程用例
critical:严重,对应次要业务主流程用例,重要业务的次要场景用例
normal:一般,对应次要业务的次要场景用例,单业务场景正常场景用例
minor:轻微,对应单业务次要场景用例
trivial:提示,对应其它级别较低的用例
8.@allure.step():定义测试步骤,可单独修饰测试操作步骤的函数,也可以直接在测试用例使用:with
allure.step(): 这样去描述测试步骤
9.@allure.description():定义用例的描述信息,修饰测试用例的方法,一般不建议使用,关于测试用
例的描述,可以在测试用例的方法里面使用文档字符串的说明方式进行说明
10.allure.attach.file():添加附件到测试报告
初级最佳实践如下:
import pytest,allure,os
@allure.epic('EcShop项目测试WebUI自动化测试')
@allure.feature('登录模块')
class TestLogin:
@allure.story('登录成功')
@allure.title('正确用户名,正确密码,登录成功')
@allure.issue('https://www.baidu.com','点击进入提单入口')
@allure.testcase('https://www.jd.com','点击进入用例管理工具入口')
@allure.severity('blocker')
def test_login(self):
'''
使用文档字符串,可以起到@allure.description()在作用
前置处理:打开浏览器
输入用户名
输入密码
断言
后置处理:退出关闭浏览器
'''
with allure.step('输入用户名'):
print('username')
with allure.step('输入密码'):
print('password')
with allure.step('点击登录按钮'):
print('点击login')
with allure.step('断言'):
print('断言是否登录成功')
if __name__ == '__main__':
pytest.main(['--alluredir=report/tmp','--clean-alluredir'])
os.system('allure generate ./report/tmp -o ./report/html --clean')
4、特性高级应用
1.在测试用例方法中可以使用:allure.dynamic.XXXX来 动态生成相关的内容,结合
@pytest.mark.parametrize()实现动态的参数化显示, 具体实践如下:
from pathlib import Path
import pytest,allure,os
from public.get_data import get_yaml_data
@allure.epic('一级模块:登录/注册/登出')
@allure.feature('二级模块:登录')
class TestLogin:
case_data_path = Path(__file__).parent.parent / 'data/login.yaml'
@pytest.mark.parametrize('kwargs', get_yaml_data(case_data_path))
@allure.issue('https://www.baidu.com','点击进入提单入口')
@allure.testcase('https://www.jd.com','点击进入用例管理工具入口')
def test_login(self,brower,kwargs):
'''
1.打开浏览器,访问登录界面
2.输入用户名
3.输入密码
4.点击登录
5.断言
'''
allure.dynamic.story(kwargs['story'])
allure.dynamic.title(kwargs['title'])
allure.dynamic.severity(kwargs['severity'])
driver = brower
with allure.step('进入登录页面:http://47.113.104.130:9966/user.php'):
driver.get(r'http://47.113.104.130:9966/user.php')
with allure.step('输入用户名:{}'.format(kwargs['username'])):
driver.find_element('name','username').send_keys(kwargs['username'])
with allure.step('输入密码:{}'.format(kwargs['password'])):
driver.find_element('name','password').send_keys(kwargs['password'])
with allure.step('点击登录按钮'):
driver.find_element('name','submit').click()
with allure.step('获取断言的实际值'):
actual_value = driver.find_element(*kwargs['assert_el']).text
with allure.step('断言'):
assert kwargs['expect_value'] in actual_value
if __name__ == '__main__':
pytest.main([__file__,'--alluredir=../report/tmp','--clean-alluredir'])
os.system('allure generate ../report/tmp -o ../report/html --clean')
2.allure测试报告默认没有环境进行,我们可以通过配置文 件添加相关信息到allure报告,具体操作:
a.在config目录中添加配置文件:evnironment.properties
b.文件格式为k=v格式,内容可配置为:
systemVersion=Win11
pythonVersion=3.8.10
allureVersion=2.18.1
baseUrl=http://47.113.104.130:9966
projectName=VSS
author=rebort
emial=rebort.chen@howentech.com
c.在运行测试用例后,打开报告前,将配置文件copy到测试结果的收集目录:result/tmp
3、错误报告截图处理
具体的处理方案如下
在测试用例中使用异常处理方式,将测试步骤包起来,使用try...expect...raise处理
import pytest,allure,os
from config.config import base_path
from public.get_data import get_yaml_data
from public.get_png_path import get_png_name
@allure.epic('注册/登录/登出')
@allure.feature('登录')
class TestLogin:
case_data_path = base_path / 'data/login.yaml'
@pytest.mark.parametrize('kwargs', get_yaml_data(case_data_path))
@allure.issue('https://www.baidu.com','点击进入提单入口')
@allure.testcase('https://www.jd.com','点击进入用例管理工具入口')
def test_login(self,brower,kwargs):
'''
1.打开浏览器,访问登录界面
2.输入用户名
3.输入密码
4.点击登录
5.断言
'''
allure.dynamic.story(kwargs['story'])
allure.dynamic.title(kwargs['title'])
allure.dynamic.severity(kwargs['severity'])
driver = brower
try:
with allure.step('进入登录页
面:http://47.113.104.130:9966/user.php'):
driver.get(r'http://47.113.104.130:9966/user.php')
with allure.step('输入用户名:{}'.format(kwargs['username'])):
driver.find_element('name','username').send_keys(kwargs['username'])
with allure.step('输入密码:{}'.format(kwargs['password'])):
driver.find_element('name','password').send_keys(kwargs['password'])
with allure.step('点击登录按钮'):
driver.find_element('name','submit').click()
with allure.step('获取断言的实际值'):
actual_value = driver.find_element(*kwargs['assert_el']).text
with allure.step('断言'):
assert kwargs['expect_value'] in actual_value
except Exception as msg: #收集异常
png_name = get_png_name() #调用public中的公共方法准备截图的图片名称
driver.save_screenshot(png_name)#截图
allure.attach.file(png_name,attachment_type=allure.attachment_type.PNG)#添加截图
附件到测试报告
raise msg #抛出异常,不然断言就错了
if __name__ == '__main__':
pytest.main(['--alluredir=../result/tmp','--clean-alluredir'])
os.system('allure generate ../result/tmp -o ../report --clean')