前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Pytest系列(10) - fixture 传参数 request的详细使用

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

作者头像
小菠萝测试笔记
发布2020-06-09 15:59:32
2.9K0
发布2020-06-09 15:59:32
举报
文章被收录于专栏:自动化、性能测试

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

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

前言

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

案例一:传单个参数

代码语言:javascript
复制
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} ")

执行结果

代码语言:javascript
复制
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 

知识点

  • 添加 参数是为了把 login 当成一个函数去执行,而不是一个参数,并且将data当做参数传入函数

indirect=True

  • ,这里的login是获取fixture返回的值

def test_name(login)

案例二:多个参数

代码语言:javascript
复制
@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']}")

执行结果

代码语言:javascript
复制
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(只加一个装饰器)

这种更常用

代码语言:javascript
复制
# 多个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)

执行结果

代码语言:javascript
复制
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(叠加装饰器)

代码语言:javascript
复制
# 多个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)

执行结果

代码语言:javascript
复制
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条

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-04-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 案例一:传单个参数
    • 执行结果
      • 知识点
      • 案例二:多个参数
        • 执行结果
          • 知识点
          • 案例三:多个fixture(只加一个装饰器)
            • 执行结果
            • 案例四:多个fixture(叠加装饰器)
              • 执行结果
              相关产品与服务
              访问管理
              访问管理(Cloud Access Management,CAM)可以帮助您安全、便捷地管理对腾讯云服务和资源的访问。您可以使用CAM创建子用户、用户组和角色,并通过策略控制其访问范围。CAM支持用户和角色SSO能力,您可以根据具体管理场景针对性设置企业内用户和腾讯云的互通能力。
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档