千纸鹤

  博客园  ::  :: 新随笔  ::  ::  :: 管理
  5 随笔 :: 70 文章 :: 0 评论 :: 9301 阅读
《一》Allure添加用例步骤详解
(1)在测试用例中添加详细的步骤有助于更好的阅读用例,也方便报错后迅速的定位问题 例子: 用例步骤:1.登陆,2.浏览商品 3.添加购物车 4.生成订单 5.支付成功
(2)第一种方式
<-----common_function.py----->
将用例步骤做关键字封装
#coding=utf-8
import allure
import pytest

def login(username,password):
"""登陆"""
print("前置操作:先登陆")

def open_goods():
"""浏览商品"""
print("浏览商品")

def add_shopping_cart(goods_id="10086"):
"""添加购物车"""
print("添加购物车")

def buy_goods():
"""生成订单"""
print("buy")

def pay_goods():
"""支付"""
print("pay")

<-----test_allure_step.py----->
开始用例设计,登陆单独拿出来,做成前置操作,后面的步骤合起来就是一个用例
#coding=utf-8
import os
import allure
import pytest
from test010.test0102.p05_testcase_step.common_function import *

@pytest.fixture(scope="session")
def login_fix():
with allure.step("setup:登陆"):
login("zz","123456")

@allure.feature("购物模块")
@allure.story("子模块")
@allure.title("流程性用例,添加测试步骤")
def test_add_goods_and_buyg(login_fix):
"""
用例步骤:
1.登陆,
2.浏览商品
3.添加购物车
4.生成订单
5.支付成功
"""
with allure.step("step1:浏览商品"):
open_goods()

with allure.step("step2:添加购物车"):
add_shopping_cart()
assert 1 == 2

with allure.step("step3:生成订单"):
buy_goods()

with allure.step("step4:支付"):
pay_goods()

with allure.step("断言"):
assert 1 == 1

if __name__ == '__main__':
pytest.main(['-s','test_allure_step.py','--alluredir','./result','--clean-alluredir'])
os.system('allure serve result')

(3)第二种方式
测试步骤也可以在函数上加上装饰器来实现:@allure.step()
<-----common_function2.py----->
#coding=utf-8
import allure
import pytest

@allure.step("setup:登陆")
def login(username,password):
"""登陆"""
print("前置操作:先登陆")

@allure.step("step:浏览商品")
def open_goods():
"""浏览商品"""
print("浏览商品")

#注意这里的参数会在报告里展示
@allure.step("step添加购物车")
def add_shopping_cart(goods_id="10086"):
"""添加购物车"""
print("添加购物车")

@allure.step("step:生成订单")
def buy_goods():
"""生成订单"""
print("buy")

@allure.step("step:支付")
def pay_goods():
"""支付"""
print("pay")

<-----test_allure_stepx.py----->
#coding=utf-8
import os
import allure
import pytest
from test010.test0102.p05_testcase_step.common_function2 import *

@pytest.fixture(scope="session")
def login_fix():
with allure.step("setup:登陆"):
login("zz","123456")

@allure.feature("购物模块")
@allure.story("子模块")
@allure.title("流程性用例,添加测试步骤")
def test_add_goods_and_buyg(login_fix):
"""
用例步骤:
1.登陆,
2.浏览商品
3.添加购物车
4.生成订单
5.支付成功
"""
open_goods()
add_shopping_cart()
buy_goods()
pay_goods()
assert 1 == 1

if __name__ == '__main__':
pytest.main(['-s','test_allure_stepx.py','--alluredir','./result','--clean-alluredir'])
os.system('allure serve result')

(4)两种方式对比
使用 with allure.step("step:步骤") 这种方式代码可读性更好一点, 但不会带上函数里面的传参和对应的值。
使用 @allure.step("step:步骤") 这种方式会带上函数的传参和对应的值。 这两种方式结合起来使用,才能更好的展示测试报告!

《二》参数化结合allure.title()生成不同标题报告
(1)参数化parametrize
<-----test_a.py----->
#coding=utf-8
import os
import pytest
import allure

def login(username,password):
"""登陆"""
print("输入账户: %s" % username)
print("输入密码: %s" % password)
# 返回
return {"code":0,"msg":"success!"}

# 测试数据
test_datas = [
({"username":"zz1","password":"123456"},"success!"),
({"username":"zz2","password":"123456"},"failed!"),
({"username":"zz3","password":"123456"},"success!")
]

@allure.story("登陆接口")
@pytest.mark.parametrize("test_input,expected",test_datas)
def test_login(test_input,expected):
"""测试登陆用例"""
# 获取函数返回结果
result = login(test_input["username"],test_input["password"])
# 断言
assert result["msg"] == expected

if __name__ == '__main__':
pytest.main(["test_a.py",'--alluredir','./result','--clean-alluredir'])
os.system('allure generate ./result/ -o ./report_allure/ --clean')

(2)param+ids参数
<-----test_a_ids.py----->
#coding=utf-8
import os
import pytest
import allure

def login(username,password):
"""登陆"""
print("输入账户: %s" % username)
print("输入密码: %s" % password)
# 返回
return {"code":0,"msg":"success!"}

# 测试数据
test_datas = [
({"username":"zz1","password":"123456"},"success!"),
({"username":"zz2","password":"123456"},"failed!"),
({"username":"zz3","password":"123456"},"success!")
]

@allure.story("登陆接口")
@pytest.mark.parametrize("test_input,expected",
test_datas,
ids=[
"输入正确账号A,密码,登陆",
"输入错误账号B,密码,登陆",
"输入正确账号C,密码,登陆",
])
def test_login(test_input,expected):
"""测试登陆用例"""
# 获取函数返回结果
result = login(test_input["username"],test_input["password"])
# 断言
assert result["msg"] == expected

if __name__ == '__main__':
pytest.main(["test_a_ids.py",'--alluredir','./result2','--clean-alluredir'])
os.system('allure generate ./result2/ -o ./report_allure2/ --clean')
<-----conftest.py----->
#coding=utf-8
def pytest_collection_modifyitems(items):
"""
测试用例收集完成时,将收集到的item的name和nodeid的中文显示在控制台上
"""
for item in items:
item.name = item.name.encode("utf-8").decode("unicode_escape")
item._nodeid = item.nodeid.encode("utf-8").decode("unicode_escape")

(3)allure.title描述用例
<-----test_a_title.py----->
#coding=utf-8
import os
import pytest
import allure

def login(username,password):
"""登陆"""
print("输入账户: %s" % username)
print("输入密码: %s" % password)
# 返回
return {"code":0,"msg":"success!"}

# 测试数据
test_datas = [
({"username":"zz1","password":"123456"},"success!"),
({"username":"zz2","password":"123456"},"failed!"),
({"username":"zz3","password":"123456"},"success!")
]

"""
使用@allure.title()时,可以加上传入的参数,
比如test_input,可以拼接test_input的值
"""
@allure.story("登陆接口")
@allure.title("用例描述,测试输入:{test_input}")
@pytest.mark.parametrize("test_input,expected",
test_datas)
def test_login(test_input,expected):
"""测试登陆用例"""
# 获取函数返回结果
result = login(test_input["username"],test_input["password"])
# 断言
assert result["msg"] == expected

if __name__ == '__main__':
pytest.main(["test_a_title.py",'--alluredir','./result3','--clean-alluredir'])
os.system('allure generate ./result3/ -o ./report_allure3/ --clean')

(4)优化用例title
<-----test_a_title_new.py----->
#coding=utf-8
import os
import pytest
import allure

def login(username,password):
"""登陆"""
print("输入账户: %s" % username)
print("输入密码: %s" % password)
# 返回
return {"code":0,"msg":"success!"}

# 测试数据
test_datas = [
({"username":"zz1","password":"123456"},"success!","输入正确账号,密码,登陆"),
({"username":"zz2","password":"123456"},"failed!","输入错误账号,密码,登陆"),
({"username":"zz3","password":"123456"},"success!","输入正确账号,密码,登陆")
]

"""
使用@allure.title()时,可以加上传入的参数,
比如test_input,可以拼接test_input的值
"""
@allure.story("登陆接口")
@allure.title("{test_input}")
@pytest.mark.parametrize("test_input,expected,title",
test_datas)
def test_login(test_input,expected,title):
"""测试登陆用例"""
# 获取函数返回结果
result = login(test_input["username"],test_input["password"])
# 断言
assert result["msg"] == expected

if __name__ == '__main__':
pytest.main(["test_a_title_new.py",'--alluredir','./result4','--clean-alluredir'])
os.system('allure generate ./result4/ -o ./report_allure4/ --clean')

《三》allure报告清空上一次记录
(1)allure报告可以记录用例每次执行的情况,方便跟踪用例的成功率,数据保留到json文件 中
(2)会带来一个问题,当你代码里面的用例删除或者更换名称后,依然会记录之前的用例报告
(3)--clean-alluredir 每次用例执行之前先清空allure的报告记录
<-----test_a.py----->
#coding=utf-8
import os
import pytest
import allure

def login(username,password):
"""登陆"""
print("输入账户: %s" % username)
print("输入密码: %s" % password)
# 返回
return {"code":0,"msg":"success!"}

# 测试数据
test_datas = [
({"username":"zz1","password":"123456"},"success!"),
({"username":"zz2","password":"123456"},"failed!"),
({"username":"zz3","password":"123456"},"success!")
]

# @allure.story("登陆接口")
# @pytest.mark.parametrize("test_input,expected",test_datas)
# def test_login(test_input,expected):
# """测试登陆用例"""
# # 获取函数返回结果
# result = login(test_input["username"],test_input["password"])
# # 断言
# assert result["msg"] == expected

def test_case02():
assert 1 == 1

if __name__ == '__main__':
# 01.allure报告可以记录用例每次执行的情况,方便跟踪用例的成功率,数据保留到json文件中
# pytest.main(["test_a.py",'--alluredir','./result'])
# 02.会带来一个问题,当你代码里面的用例删除或者更换名称后,依然会记录之前的用例报告
# 03.--clean-alluredir 每次用例执行之前先清空allure的报告记录
pytest.main(["test_a.py", '--alluredir', './result','--clean-alluredir'])
# 04.生成报告文件到工程
# os.system('allure generate ./result/ -o ./report_allure/')
# 05. 这里加clean只是让报告可重新生成,生成的结果,会保留之前用例执行的记录
# (json文件每次执行都会不断增加),如果不加,同一目录只能生成一次,然后提示无法生成
# os.system('allure generate ./result/ -o ./report_allure/ --clean')
os.system('allure serve result')

《四》allure动态生成用例标题
(1)allure.dynamic 常用动态属性
feature 模块 allure.dynamic.feature(feature_name)
功能点 story allure.dynamic.story(case_story)
用例标题 title allure.dynamic.title(case_title)
(2)description 用例描述动态更新
(3)title 用例标题动态更新
(4)参数化时,使用@allure.title给用例不同标题
<-----test_case_01.py----->
import os
import allure
import pytest

# 接口用例描述例子
desc = "<font color='red'>请求URL:</font>{}<Br/>" \
"<font color='red'>请求类型:</font>{}<Br/>" \
"<font color='red'>期望结果:</font>{}<Br/>" \
"<font color='red'>实际结果描述:</font>{}<Br/>"\
.format("http://www.baidu.com","post","200","404")

# description 用例描述动态更新
@allure.description("原始描述")
def test_dynamic_description():
# 断言成功后,可以在测试主体内部动态更新描述
assert 42 == int(6 * 7)
# 断言失败,不会动态更新描述
# assert 43 == int(6 * 7)
allure.dynamic.description(desc)

# title 用例标题动态更新
@allure.title("原始标题")
def test_with_dynamic_title():
assert 2 + 2 == 4
allure.dynamic.title('当断言成功时,用例标题会动态更新')

# 参数化时,使用@allure.title给用例不同标题
@allure.title("参数化用例标题: 添加 {param1} 和 {param2}")
@pytest.mark.parametrize('param1,param2,expected', [
(2, 2, 4),
(1, 2, 5)
])
def test_with_parameterized_title(param1, param2, expected):
assert param1 + param2 == expected

if __name__ == '__main__':
# pytest.main(['-s',"test_case_01.py",'--alluredir','./result'])
pytest.main(['-s', "test_case_01.py", '--alluredir', './result','--clean-alluredir'])
# os.system('allure generate ./result/ -o ./report_allure/ --clean')
os.system('allure serve result')

(5)参数化时,在用例里使用allure.dynamic.title更新用例的标题
<-----test_case_02.py----->
#coding=utf-8
import os
import pytest
import allure

def login(username,password):
"""登陆"""
print("输入账户: %s" % username)
print("输入密码: %s" % password)
# 返回
return {"code":0,"msg":"success!"}

# 测试数据
test_datas = [
({"username":"zz1","password":"123456"},"success!","输入正确账号,密码,登陆"),
({"username":"zz2","password":"123456"},"failed!","输入错误账号,密码,登陆"),
({"username":"zz3","password":"123456"},"success!","输入正确账号,密码,登陆")
]

"""
使用@allure.title()时,可以加上传入的参数,
比如test_input,可以拼接test_input的值
"""
@allure.story("登陆接口")
@pytest.mark.parametrize("test_input,expected,title",
test_datas)
def test_login(test_input,expected,title):
"""测试登陆用例"""
# 获取函数返回结果
result = login(test_input["username"],test_input["password"])
# 断言
assert result["msg"] == expected
# 断言成功的用例,动态更新标题
allure.dynamic.title(title)

if __name__ == '__main__':
pytest.main(["test_case_02.py",'--alluredir','./result4','--clean-alluredir'])
os.system('allure serve result4')

《五》allure生成环境配置
(1)allure报告默认没有环境信息
(2)添加环境信息方式一:environment.properties
在allure的result目录(json数据里的)下添加一个 environment.properties 文件(配置文件不支持中文,有中文会有乱码)
environment.properties 文件格式
systemVersion=win10
pythonVersion=3.6.0
allureVersion=2.13.0
baseUrl=http://192.168.1.x:8080
projectName=test
author=zz
email=123123123@qq.com

(3)添加环境信息方式二:environment.xml
在allure的result目录(json数据里的)下添加一个environment.xml 文件
environment.xml 文件格式
<environment>
<parameter>
<key>Browser</key>
<value>Chrome</value>
</parameter>
<parameter>
<key>Browser.Version</key>
<value>63.0</value>
</parameter>
<parameter>
<key>Stand</key>
<value>Production</value>
</parameter>
</environment>

(4)通过copy命令解决配置文件被删除问题
(1)在运行 pytest 生成 allure 报告的时候,偶尔需要加 --clean 参数,清除之前的报告记 录,这样会之前清空 result 目录,environment.properties文件也会被删除
(2)为了不让 environment.properties 文件删除掉,可以把 environment.properties 文件放 项目根目录,在运行报告的时候,先 copy 到 report 目录
<-----test_case_02.py----->
#coding=utf-8
import os
import pytest
import allure

def login(username,password):
"""登陆"""
print("输入账户: %s" % username)
print("输入密码: %s" % password)
# 返回
return {"code":0,"msg":"success!"}

# 测试数据
test_datas = [
({"username":"zz1","password":"123456"},"success!","输入正确账号,密码,登陆"),
({"username":"zz2","password":"123456"},"failed!","输入错误账号,密码,登陆"),
({"username":"zz3","password":"123456"},"success!","输入正确账号,密码,登陆")
]

"""
使用@allure.title()时,可以加上传入的参数,
比如test_input,可以拼接test_input的值
"""
@allure.story("登陆接口")
@pytest.mark.parametrize("test_input,expected,title",
test_datas)
def test_login(test_input,expected,title):
"""测试登陆用例"""
# 获取函数返回结果
result = login(test_input["username"],test_input["password"])
# 断言
assert result["msg"] == expected
# 断言成功的用例,动态更新标题
allure.dynamic.title(title)

if __name__ == '__main__':
pytest.main(["test_case_02.py",'--alluredir','./result4','--clean-alluredir'])
# 把 environment.properties 文件放项目根目录,在运行报告的时候,先 copy 到 result 目录
os.system('copy environment.properties result4\\environment.properties')
os.system('allure serve result4')
posted on   隆江猪脚饭  阅读(125)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示