前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >pytest学习和使用13-Pytest的fixture如何使用request传入参数?

pytest学习和使用13-Pytest的fixture如何使用request传入参数?

原创
作者头像
虫无涯
发布2023-02-17 10:44:01
发布2023-02-17 10:44:01
75600
代码可运行
举报
文章被收录于专栏:全栈测试技术全栈测试技术
运行总次数:0
代码可运行

1 使用场景

  • 当我们为了提高用例的复用性,会用到不同的fixture,比如登陆场景;
  • 但是如果登陆场景,我们使用不同的账号进行测试,那如何来做?
  • 此时不能使用fixture把账号直接写死,需要通过传参的方式来实现。

2 传单个参数

代码语言:python
代码运行次数:0
运行
复制
# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/12/27 
# 文件名称:test_request.py
# 作用:fixture传参
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import pytest

# 传一个参数
@pytest.fixture()
def user_login(request):
    user_name = request.param
    return user_name


data = ["user_name01", "user_name02"]


@pytest.mark.parametrize("user_login", data, indirect=True)
def test_login(user_login):
    print(f"登陆用户的名称为:{user_login}")


if __name__ == '__main__':
    pytest.main(["-s", "test_request.py"])
代码语言:python
代码运行次数:0
运行
复制
test_request.py::test_login[user_name01] PASSED
[ 50%]登陆用户的名称为:user_name01

test_request.py::test_login[user_name02] PASSED   
[100%]登陆用户的名称为:user_name02


============================== 2 passed in 0.03s ==============================
  • 其中indirect=True 参数是为了把 user_login 当成一个函数去执行,而不是一个参数,并且将data当做参数传入函数。

3 传多个参数

  • 传多个参数,需要通过字典去传。
代码语言:python
代码运行次数:0
运行
复制
# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/12/27 
# 文件名称:test_request01.py
# 作用:fixture传参
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import pytest

# 传多个参数
@pytest.fixture()
def user_login(request):
    user_name = request.param
    return user_name


data = [{"user_name": "user_name01", "passwd": "passwd01"},
        {"user_name": "user_name02", "passwd": "passwd02"}
        ]


@pytest.mark.parametrize("user_login", data, indirect=True)
def test_login(user_login):
    print(f"登陆用户的名称为:{user_login['user_name']},登陆的密码: {user_login['passwd']}")


if __name__ == '__main__':
    pytest.main(["-s", "test_request01.py"])
代码语言:python
代码运行次数:0
运行
复制
test_request01.py::test_login[user_login0] PASSED   
[ 50%]登陆用户的名称为:user_name01,登陆的密码: passwd01

test_request01.py::test_login[user_login1] PASSED   
[100%]登陆用户的名称为:user_name02,登陆的密码: passwd02


============================== 2 passed in 0.04s ==============================

4 多个fixtrue

代码语言:python
代码运行次数:0
运行
复制
# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/12/27 
# 文件名称:test_request02.py
# 作用:fixture传参
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import pytest

# 传多个fixture
@pytest.fixture()
def user_name(request):
    name = request.param
    return name

@pytest.fixture()
def user_pwd(request):
    pwd = request.param
    return pwd


data = [("user_name01", "pwd01"), ("user_name02", "pwd02")]


@pytest.mark.parametrize("user_name, user_pwd", data, indirect=True)
def test_login(user_name, user_pwd):
    print(f"登陆信息为{user_name}, {user_pwd}")


if __name__ == '__main__':
    pytest.main(["-s", "test_request02.py"])
代码语言:python
代码运行次数:0
运行
复制
test_request02.py::test_login[user_name01-pwd01] PASSED      
[ 50%]登陆信息为user_name01, pwd01

test_request02.py::test_login[user_name02-pwd02] PASSED      
[100%]登陆信息为user_name02, pwd02


============================== 2 passed in 0.03s ==============================

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 使用场景
  • 2 传单个参数
  • 3 传多个参数
  • 4 多个fixtrue
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档