Pytest系列(10) - fixture 传参数 request的详细使用

简介: Pytest系列(10) - fixture 传参数 request的详细使用

如果你还想从头学起Pytest,可以看看这个系列的文章哦!

https://www.cnblogs.com/poloyy/category/1690628.html

 

前言


  • 为了提高复用性,我们在写测试用例的时候,会用到不同的fixture,比如:最常见的登录操作,大部分的用例的前置条件都是登录
  • 假设不同的用例想登录不同的测试账号,那么登录fixture就不能把账号写死,需要通过传参的方式来完成登录操作

 

案例一:传单个参数


import pytest
@pytest.fixture()
def login(request):
    name = request.param
    print(f"== 账号是:{name} ==")
    return name
data = ["pyy1", "polo"]
ids = [f"login_test_name is:{name}" for name in data]
@pytest.mark.parametrize("login", data, ids=ids, indirect=True)
def test_name(login):
    print(f" 测试用例的登录账号是:{login} ")


执行结果

collecting ... collected 2 items
10fixture_request.py::test_name[login_test_name is:pyy1] == 账号是:pyy1 ==
PASSED          [ 50%] 测试用例的登录账号是:pyy1 
10fixture_request.py::test_name[login_test_name is:polo] == 账号是:polo ==
PASSED          [100%] 测试用例的登录账号是:polo 


知识点

  • 添加  indirect=True  参数是为了把 login 当成一个函数去执行,而不是一个参数,并且将data当做参数传入函数
  • def test_name(login) ,这里的login是获取fixture返回的值

 

案例二:多个参数


@pytest.fixture()
def logins(request):
    param = request.param
    print(f"账号是:{param['username']},密码是:{param['pwd']}")
    return param
data = [
    {"username": "name1", "pwd": "pwd1"},
    {"username": "name2", "pwd": "pwd2"},
]
@pytest.mark.parametrize("logins", data, indirect=True)
def test_name_pwd(logins):
    print(f"账号是:{logins['username']},密码是:{logins['pwd']}")


执行结果

10fixture_request.py::test_name_pwd[logins0] 账号是:name1,密码是:pwd1
PASSED                      [ 50%]账号是:name1,密码是:pwd1
10fixture_request.py::test_name_pwd[logins1] 账号是:name2,密码是:pwd2
PASSED                      [100%]账号是:name2,密码是:pwd2


知识点

如果需要传多个参数,需要通过字典去传

 

案例三:多个fixture(只加一个装饰器)


这种更常用

# 多个fixture
@pytest.fixture(scope="module")
def input_user(request):
    user = request.param
    print("登录账户:%s" % user)
    return user
@pytest.fixture(scope="module")
def input_psw(request):
    psw = request.param
    print("登录密码:%s" % psw)
    return psw
data = [
    ("name1", "pwd1"),
    ("name2", "pwd2")
]
@pytest.mark.parametrize("input_user,input_psw", data, indirect=True)
def test_more_fixture(input_user, input_psw):
    print("fixture返回的内容:", input_user, input_psw)


执行结果

10fixture_request.py::test_more_fixture[name1-pwd1] 登录账户:name1
登录密码:pwd1
PASSED               [ 50%]fixture返回的内容: name1 pwd1
10fixture_request.py::test_more_fixture[name2-pwd2] 登录账户:name2
登录密码:pwd2
PASSED               [100%]fixture返回的内容: name2 pwd2


案例四:多个fixture(叠加装饰器)


# 多个fixture
@pytest.fixture(scope="function")
def input_user(request):
    user = request.param
    print("登录账户:%s" % user)
    return user
@pytest.fixture(scope="function")
def input_psw(request):
    psw = request.param
    print("登录密码:%s" % psw)
    return psw
name = ["name1", "name2"]
pwd = ["pwd1", "pwd2"]
@pytest.mark.parametrize("input_user", name, indirect=True)
@pytest.mark.parametrize("input_psw", pwd, indirect=True)
def test_more_fixture(input_user, input_psw):
    print("fixture返回的内容:", input_user, input_psw)


执行结果

10fixture_request.py::test_more_fixture[pwd1-name1] 登录账户:name1
登录密码:pwd1
PASSED               [ 25%]fixture返回的内容: name1 pwd1
10fixture_request.py::test_more_fixture[pwd1-name2] 登录账户:name2
登录密码:pwd1
PASSED               [ 50%]fixture返回的内容: name2 pwd1
10fixture_request.py::test_more_fixture[pwd2-name1] 登录账户:name1
登录密码:pwd2
PASSED               [ 75%]fixture返回的内容: name1 pwd2
10fixture_request.py::test_more_fixture[pwd2-name2] 登录账户:name2
登录密码:pwd2
PASSED               [100%]fixture返回的内容: name2 pwd2

测试用例数=2*2=4条

相关文章
|
8月前
|
编译器 开发工具 C语言
【2026最新】CLion下载安装和汉化保姆级教程(超级肝,新手必看)
CLion 是 JetBrains 推出的专业 C/C++ 跨平台 IDE,支持 Windows/macOS/Linux,集成智能代码补全、实时分析、GDB/LLDB 调试、CMake 构建及 Git 版本控制,兼容 GCC/Clang/MSVC 编译器,大幅提升开发效率。(239 字)
|
XML 测试技术 数据库
深入理解自动化测试中的数据驱动方法
【5月更文挑战第29天】 在现代软件测试实践中,自动化不仅提升了效率,也提出了更高要求。数据驱动测试(DDT)作为一种高效的自动化测试策略,通过分离测试逻辑和测试数据,增强了测试案例的可维护性和可扩展性。本文将详细探讨数据驱动方法的原理、实施步骤以及面临的挑战,并展示如何利用Python和Selenium框架实现一个基本的数据驱动测试实例,以期为读者提供一种提升自动化测试效率和灵活性的有效途径。
|
设计模式 Java 测试技术
自动化测试实战 | 搞定 PageObject 设计模式
在为 UI 页面写测试用例时(比如 Web 页面,移动端页面),测试用例会存在大量元素和操作细节。如何面对当 UI 变化时,测试用例也要跟着变化这个问题?PageObject 设计模式闪亮登场
自动化测试实战 | 搞定 PageObject 设计模式
|
测试技术
Pytest----fixture基础应用
Pytest----fixture基础应用
427 0
|
设计模式 前端开发 测试技术
Selenium系列(二十) - PageObject模式的详细介绍
Selenium系列(二十) - PageObject模式的详细介绍
564 0
|
1天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8058 2
|
10天前
|
域名解析 人工智能 Linux
|
20天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18307 63
|
18天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
4天前
|
人工智能 安全 网络安全