pytest + yaml 框架 -4.用例参数化parameters功能实现
2023-06-13 09:16:27 时间
前言
当一个用例用到多组测试数据的时候,我们必然会用到参数化,接下来看下如何在yaml文件中实现参数化
pip 安装插件
pip install pytest-yaml-yoyo
参数化功能在v1.0.3版本上实现
参数化的实现
用例参数化的实现,我设计了2种实现方式
参数化方式1:
config:
name: post示例
fixtures: username, password
parameters:
- [test1, '123456']
- [test2, '123456']
参数化方式2:
config:
name: post示例
parameters:
- {"username": "test1", "password": "123456"}
- {"username": "test2", "password": "1234562"}
使用 fixtures 功能实现参数化
基本实现原理参考pytest框架的参数化实现
import pytest
@pytest.mark.parametrize("test_input,expected",
[ ["3+5", 8],
["2+4", 6[,
["6 * 9", 42[,
])
def test_eval(test_input, expected):
assert eval(test_input) == expected
在上面的用例中,只需要关注参数化的2个变量test_input, expected 也就是在测试用例中传的2个值,可以理解为用例的2个fixture参数 还需要关注测试数据,测试数据结构必须是list,里面是可以迭代的数据,因为有2个变量,所以每组数据是2个值。
在yaml文件中
- 参数化需要的变量写到config的fixtures 位置
- 参数化需要的数据写到 parameters
示例 test_params.yml
# 作者-上海悠悠 微信/QQ交流:283340479
# blog地址 https://www.cnblogs.com/yoyoketang/
config:
name: post示例
fixtures: username, password
parameters:
- [test1, '123456']
- [test2, '123456']
teststeps:
-
name: post
request:
method: POST
url: http://httpbin.org/post
json:
username: ${username}
password: ${password}
extract:
url: body.url
validate:
- eq: [status_code, 200]
- eq: [headers.Server, gunicorn/19.9.0]
- eq: [$..username, '${username}']
- eq: [body.json.username, '${username}']
运行yaml文件
pytest test_params.yml
会自动生成2个测试用例
(venv) D:\code\tests>pytest test_params.yml
======================== test session starts ========
platform win32 -- Python 3.8.5, pytest-7.2.0, pluggy-1.0.0
rootdir: D:\code\pytest-yaml-yoyo
plugins: yaml-yoyo-1.0.3
collected 2 items
test_params.yml .. [100%]
=================== 2 passed in 0.77s ================
parameters 实现参数化
第二种实现方式,可以在fixtures 中传变量,但是测试数据必须是字典类型,从字典的key中动态读取变量名称 test_params_2.yml
# 作者-上海悠悠 微信/QQ交流:283340479
# blog地址 https://www.cnblogs.com/yoyoketang/
config:
name: post示例
parameters:
- {"username": "tes1", "password": "123456"}
- {"username": "tes2", "password": "123456"}
teststeps:
-
name: post
request:
method: POST
url: http://httpbin.org/post
json:
username: ${username}
password: ${password}
extract:
url: body.url
validate:
- eq: [status_code, 200]
- eq: [headers.Server, gunicorn/19.9.0]
- eq: [$..username, '${username}']
- eq: [body.json.username, '${username}']
运行yaml文件
pytest test_params.yml
以上2种实现参数化方式效果是一样的
相关文章
- pytest数据驱动及conftest文件及装饰器使用
- pytest运行_ios怎么清理应用缓存在哪里
- Pytest(16)随机执行测试用例pytest-random-order[通俗易懂]
- @pytest.mark.parametrize_pytest参数化可变参数
- Pytest(18)pytest接口自动化完整框架思维导图[通俗易懂]
- Pytest和Allure测试框架-超详细版+实战
- 这是我见过最牛逼的接口自动化测试框架没有之一:基于python+requests+pytest+allure实现
- 自动化冒烟测试:Unittest vs Pytest框架
- pytest测试框架和unittest_python列表生成式
- Python pytest测试框架详解
- pytest学习和使用17-Pytest如何重复执行用例?(pytest-repeat)
- 测试之路 pytest接口自动化框架-插件补充及pytest装饰器扩展
- 测试之路 pytest接口自动化框架扩展-json数据解析
- pytest + yaml 框架 - 3.全局仅登录一次,添加Authentication token认证
- pytest + yaml 框架 -13.多环境配置切换
- pytest + yaml 框架 -16.提供一些常用的内置函数和方法
- Python 自动化测试框架unittest与pytest的区别
- pytest学习和使用6-fixture如何使用?
- pytest学习和使用9-fixture中conftest.py如何使用?
- pytest学习和使用13-Pytest的fixture如何使用request传入参数?
- pytest学习和使用19-pytes断言失败后,怎样保持后续的断言继续执行?(pytest-assume)
- pytest学习和使用22-allure特性 丨总览中的Environment、Categories设置以及Flaky test使用
- pytest学习和使用25-当看到allure的动态生成标题和描述的特性时,直言简直太灵活了(allure.dynamic使用)