蟑螂恶霸的博客 蟑螂恶霸的博客
首页
  • Web自动化
  • 自动化测试框架
  • 接口自动化
  • 测试面试题
  • 技术文档
  • GitHub技巧
  • 博客搭建
  • Vue
  • JavaScript
  • Nginx
  • 自动化测试
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

蟑螂恶霸

一个爱折腾的程序员,什么都想试试!
首页
  • Web自动化
  • 自动化测试框架
  • 接口自动化
  • 测试面试题
  • 技术文档
  • GitHub技巧
  • 博客搭建
  • Vue
  • JavaScript
  • Nginx
  • 自动化测试
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • web自动化

  • 自动化测试框架

  • 接口自动化测试

  • 测试面试题

  • Pytest

    • 快速入门和基础讲解
    • assert断言详细使用
    • setup和teardown的详细使用
    • fixture的详细使用
    • 测试用例执行后的几种状态
    • conftest.py的详细讲解
    • skip、skipif跳过用例
    • 使用自定义标记mark
    • 参数化@pytest.mark.parametrize
    • fixture 传参数 request的详细使用
      • 前言
      • 案例一:传单个参数
        • 执行结果
        • 知识点
      • 案例二:多个参数
        • 执行结果
        • 知识点
      • 案例三:多个fixture(只加一个装饰器)
        • 执行结果
      • 案例四:多个fixture(叠加装饰器)
        • 执行结果
    • 失败重跑插件pytest-rerunfailures的详细使用
    • 测试结果生成HTML报告插件之pytest-html的详细使用
    • 重复执行用例插件之pytest-repeat的详细使用
    • 配置文件pytest.ini的详细使用
    • 多重校验插件之pytest-assume的详细使用
    • 分布式测试插件之pytest-xdist的详细使用
    • pytest-xdist分布式测试的原理和流程
    • 超美测试报告插件之allure-pytest的基础使用
    • 我们需要掌握的allure特性
    • allure的特性,@allure.step()、allure.attach的详细使用
    • allure的特性,@allure.description()、@allure.title()的详细使用
    • allure的特性,@allure.link()、@allure.issue()、@allure.testcase()的详细使用
    • allure 打标记之 @allure.epic()、@allure.feature()、@allure.story() 的详细使用
    • allure 环境准备
    • allure.severity 标记用例级别
    • 清空 allure 历史报告记录
    • allure 命令行参数
    • 参数化 parametrize + @allure.title() 动态生成标题
    • 详解 allure.dynamic 动态生成功能
    • 使用 pytest-xdist 分布式插件,如何保证 scope=session 的 fixture 在多进程运行情况下仍然能只运行一次
  • 自动化测试
  • Pytest
蟑螂恶霸
2022-07-22
目录

fixture 传参数 request的详细使用

# 前言

  • 为了提高复用性,我们在写测试用例的时候,会用到不同的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} ")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 执行结果

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 
1
2
3
4
5
6
7

# 知识点

  • 添加 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']}")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 执行结果

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
1
2
3
4
5

# 知识点

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

# 案例三:多个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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# 执行结果

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
1
2
3
4
5
6
7

# 案例四:多个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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 执行结果

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

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

本文转自 https://www.cnblogs.com/poloyy/p/12685948.html (opens new window),如有侵权,请联系删除。

上次更新: 2022/10/15, 15:19:25
参数化@pytest.mark.parametrize
失败重跑插件pytest-rerunfailures的详细使用

← 参数化@pytest.mark.parametrize 失败重跑插件pytest-rerunfailures的详细使用→

最近更新
01
实现定时任务数据库配置
06-09
02
SQL Server连接
02-22
03
RSA加密工具类
02-22
更多文章>
Theme by Vdoing | Copyright © 2022-2023 蟑螂恶霸 | Blog
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式