Pytest 系列(28)- 参数化 parametrize + @allure.title() 动态生成标题
如果你还想从头学起Pytest,可以看看这个系列的文章哦!
https://www.cnblogs.com/poloyy/category/1690628.html
前言
- 参数化 @pytest.mark.parametrize 的学习:https://www.cnblogs.com/poloyy/p/12675457.html
- 默认 allure 报告上的测试用例标题不设置默认就是用例名称,这样可读性不高
- 当结合 @pytest.mark.parametrize 参数化完成数据驱动时,如果标题写死,这样可读性也不高
- 所以我们希望标题可以动态的生成,来看看如何做吧
参数化无标题的栗子
测试代码
#!/usr/bin/env python
# -*- coding: utf-8 -*- """
__title__ =
__Time__ = 2020/10/28 15:08
__Author__ = 小菠萝测试笔记
__Blog__ = https://www.cnblogs.com/poloyy/
"""
import allure
import pytest @pytest.fixture()
def login(request):
"""登录"""
param = request.param
print(f"账号是:{param['username']},密码是:{param['pwd']}")
# 返回
return {"code": 0, "msg": "success!"} datas = [
{"username": "name1", "pwd": "pwd1"},
{"username": "name2", "pwd": "pwd2"},
{"username": "name3", "pwd": "pwd3"}
] @allure.story('登录功能')
@pytest.mark.parametrize('login', datas, indirect=True)
def test_login1(login):
"""
登录测试用例1
"""
assert login['code'] == 0
allure 报告

标题就是方法名+参数化的数据,看着可读性就不咋滴
参数化有标题写死的栗子
测试代码
将上面的测试代码添加一个 @allure.title 就可以了
@allure.story('登录功能')
@allure.title('登录测试用例2')
@pytest.mark.parametrize('login', datas, indirect=True)
def test_login2(login):
"""
登录测试用例2
"""
assert login['code'] == 0
allure 报告

因为参数化可以生成三条用例,所以三条用例都用了同一个 title,可读性也不咋滴
参数化使用 ids 的栗子
测试代码
#!/usr/bin/env python
# -*- coding: utf-8 -*- """
__title__ =
__Time__ = 2020/10/28 15:08
__Author__ = 小菠萝测试笔记
__Blog__ = https://www.cnblogs.com/poloyy/
"""
import allure
import pytest @pytest.fixture()
def login(request):
"""登录"""
param = request.param
print(f"账号是:{param['username']},密码是:{param['pwd']}")
# 返回
return {"code": 0, "msg": "success!"} datas = [
{"username": "name1", "pwd": "pwd1"},
{"username": "name2", "pwd": "pwd2"},
{"username": "name3", "pwd": "pwd3"}
] ids = [
"username is name1,pwd is pwd1",
"username is name2,pwd is pwd2",
"username is name3,pwd is pwd3"
] @allure.story('登录功能')
@pytest.mark.parametrize('login', datas, ids=ids, indirect=True)
def test_login1(login):
"""
登录测试用例1
"""
assert login['code'] == 0
allure 报告

参数化动态生成标题的栗子
测试代码
#!/usr/bin/env python
# -*- coding: utf-8 -*- """
__title__ =
__Time__ = 2020/10/28 15:08
__Author__ = 小菠萝测试笔记
__Blog__ = https://www.cnblogs.com/poloyy/
"""
import allure
import pytest @pytest.fixture()
def login(request):
"""登录"""
param = request.param
print(f"账号是:{param['username']},密码是:{param['pwd']}")
# 返回
return {"code": 0, "msg": "success!"} datas = [
{"username": "name1", "pwd": "pwd1"},
{"username": "name2", "pwd": "pwd2"},
{"username": "name3", "pwd": "pwd3"}
] data2 = [
("name1", "123456"),
("name2", "123456"),
("name3", "123456")
] @allure.story('分别传值')
@allure.title('登录测试用例2-账号是:{username}-密码是:{pwd}')
@pytest.mark.parametrize('username,pwd', data2)
def test_login1(username, pwd):
"""
登录测试用例1
"""
print(username, pwd) @allure.story('字典参数化')
@allure.title('登录测试用例2-{dict}')
@pytest.mark.parametrize('dict', datas)
def test_login2(dict):
"""
登录测试用例1
"""
print(dict['username'], dict['pwd']) @allure.story('传值进fixture')
@allure.title('登录测试用例2{login}')
@pytest.mark.parametrize('login', datas, indirect=True)
def test_login3(login):
"""
登录测试用例2
"""
assert login['code'] == 0
allure 报告

传入的如果是一个字典则显示完整字典值
参数化动态生成标题最优方案的栗子
测试代码
#!/usr/bin/env python
# -*- coding: utf-8 -*- """
__title__ =
__Time__ = 2020/10/28 15:08
__Author__ = 小菠萝测试笔记
__Blog__ = https://www.cnblogs.com/poloyy/
"""
import allure
import pytest data = [
("name1", "123456", "name1 登录成功"),
("name2", "123456", "name2 登录失败"),
("name3", "123456", "name3 登录成功")
] @allure.story('分别传值')
@allure.title('登录测试用例-{title}')
@pytest.mark.parametrize('username,pwd,title', data)
def test_login1(username, pwd, title):
"""
登录测试用例1
"""
print(username, pwd)
allure 报告

这种做法的优点
- 可以自定义各式各样的标题
- 单独一个值去维护标题值
- 可读性比较好,容易维护
Pytest 系列(28)- 参数化 parametrize + @allure.title() 动态生成标题的更多相关文章
- Pytest 系列(29)- 详解 allure.dynamic 动态生成功能
如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 @allure.title ...
- Pytest系列(21)- allure的特性,@allure.description()、@allure.title()的详细使用
如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 前面介绍了两种allure的 ...
- Pytest系列(20)- allure结合pytest,allure.step()、allure.attach的详细使用
如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 allure除了支持pyte ...
- Pytest系列(22)- allure的特性,@allure.link()、@allure.issue()、@allure.testcase()的详细使用
如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 上一篇文章介绍了两种allu ...
- Pytest系列(23)- allure打标记,@allure.feature()、@allure.story()、@allure.severity()的详细使用
如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 前面几篇文章主要介绍了all ...
- Pytest 系列(27)- allure 命令行参数
如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 先看看 allure 命令的帮助文 ...
- Pytest 系列(24)- allure 环境准备
如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html allure 和 pytest 相 ...
- pytest文档8-参数化(parametrize)结合allure.title()生成不同标题报告
参数化parametrize 先看一个简单的pytest参数化案例演示test_a.py # test_a.py import pytest import allure def login(usern ...
- C#动态生成html页
Html生成模块:WriteHtml.cs using System.Collections.Generic; using System.IO; using System.Text; namespac ...
随机推荐
- IOS自动化测试环境搭建(Python & Java)
一.前言 IOS的App自动化测试与Android的一样,也可以用appium来进行.但是IOS自动化依赖苹果的osx系统.Xcode构建等,且封闭的系统需要苹果开发者账号才可以驱动真机.A ...
- SpringCloud-Alibaba 最新的 依赖版本管理组合以及 整合gateway遇到的问题
一般来说,cloud 与 alibaba 拥有版本组合说明文档wiki,不过这里可以做一个最新的组合的分享. <dependencyManagement> <dependencies ...
- SpringSecurity:简单入门
SpringSecurity能做什么 SpringSecurity是一个安全框架,使用它可以让我们的系统变得安全一点,它可以对登陆系统的用户进行验证和授权 一个安全的系统需要做的事情很多,比如:防SQ ...
- MongoDB实例重启失败探究(大事务Redo导致)
1.实例重启背景 收到监控组同学反馈,连接某一个MongoDB实例的应用耗时异常,并且出现了超时.查看数据库监控平台,发现此实例服务器的IO异常飙升,而查看副本集状态(rs.status()),主从是 ...
- 值得收藏 | 深度剖析 TensorCore 卷积算子实现原理
作者:章晓 | 旷视 MegEngine 架构师 一.前言 2020 年 5 月 Nvidia 发布了新一代的 GPU 架构安培(Ampere).其中和深度学习关系最密切的莫过于性能强劲的第三代的 T ...
- 【XXE学习】XML外部实体注入
一.XML外部实体注入介绍 1.1 XXE简介 XML外部实体注入(XML External Entity Injection)也就是人们(mian shi guan )常说的XXE啦,见名知意,就是 ...
- C#/VB.NET 将PDF转为OFD
OFD,全称Open Fixed-layout Document ,是一种可存储.读取以及编辑的国家标准版式的电子文档格式,属于中国的一种自主文件格式,在安全性上有可靠保证.为突破国外技术在我们软硬件 ...
- Golang在Linux系统中实现微秒级延迟
在程序中延迟或者等待一段时间一般可以使用Sleep函数实现,但是因为操作系统线程调度的消耗,往往只能做到十几或者数十毫秒的精度,很难达到微秒级,Golang的time.Sleep也是如此. Sleep ...
- redis缓存穿透,缓存击穿,缓存雪崩
缓存穿透 缓存穿透是指用户查询数据,在数据库没有,自然在缓存中也不会有.这样就导致用户查询的时候,在缓存中找不到,每次都要去数据库再查询一遍,然后返回空(相当于进行了两次无用的查询).这样请求就会绕过 ...
- maven打包war,导入本地jar包
方法1: 一 . 在项目根目录创建lib文件夹,把jar放入lib文件夹中 二 . 在项目中使用本地jar pom文件配置如下: <properties> <project.buil ...