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 ...
随机推荐
- Dapr 客户端 搭配 WebApiClientCore 玩耍服务调用
使用Dapr 客户端 处理服务调用,需要遵循的他的模式,通常代码是这个样子的: var client = DaprClient.CreateInvokeHttpClient(appId: " ...
- FreeRTOS-04-内核控制函数+时间管理函数
说明 本文仅作为学习FreeRTOS的记录文档,作为初学者肯定很多理解不对甚至错误的地方,望网友指正. FreeRTOS是一个RTOS(实时操作系统)系统,支持抢占式.合作式和时间片调度.适用于微处理 ...
- C# / vb.net 给PDF 添加可视化和不可见数字签名
本文通过C#程序代码展示如何给PDF文档添加可视化数字签名和不可见数字签名.可视化数字签名,即在PDF文档中的指定页面位置添加签名,包含相关文字信息和签名图片等:不可见数字签名,即添加签名时不在文档中 ...
- House_of_orange 学习小结
House_of_orange学习小结 house_of_orange最早出现在2016年hitcon的一道同名题目,其利用效果,是当程序没有free函数的时候,我们可以通过一些方法,来让chunk被 ...
- 你的ES数据备份了吗?
前言: 无论使用哪种存储软件,定期的备份数据都是重中之重,在使用ElasticSearch的时候,随着数据日益积累,存放es数据的磁盘空间也捉襟见肘, 此时对于业务功能使用不到的索引数据,又不能直接删 ...
- rancher清理主机脚本
#!/bin/bash #From:rancher #date:2019-10-18 #admin:jarno # 停止服务 systemctl disable kubelet.service sys ...
- 使用Cobertura做代码覆盖率测试
经验总结:首先要把cobertura.jar包含ant的classpath路径中,其次要求它包含在测试用例的classpath中: 使用cobertura做代码覆盖率测试中出现的问题:覆盖率始终为0, ...
- MVC配置原理-源码
目录 举例 修改SpringBoot的默认配置 全面接管SpringMVC 参考链接 在进行项目编写前,我们还需要知道一个东西,就是SpringBoot对我们的SpringMVC还做了哪些配置,包括如 ...
- kali2020更换JDK&&安装burpsuite pro
写在前面 之前因为安装maven把JDK换成了1.8.0_261,尝试诸多方法依然打不开自带的burp,正好在做CTF做不出来 QAQ,一气之下打算安个破解版burp. 安装 0x00 更换JDK 使 ...
- @CreatedDate@CreatedBy@LastModifiedBy@LastModifiedDate
启动类上加上@EnableJpaAuditing 实体类,注意需要加上@EntityListeners(AuditingEntityListener.class)这个注解才能使@CreatedDate ...