如果你还想从头学起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条

Pytest系列(10) - firture 传参数 request的详细使用的更多相关文章

  1. pytest文档14-函数传参和firture传参数request

    前言 为了提高代码的复用性,我们在写用例的时候,会用到函数,然后不同的用例去调用这个函数. 比如登录操作,大部分的用例都会先登录,那就需要把登录单独抽出来写个函数,其它用例全部的调用这个登陆函数就行. ...

  2. pytest十一:函数传参和 firture 传参数 request

    为了提高代码的复用性,我们在写用例的时候,会用到函数,然后不同的用例去调用这个函数.比如登录操作,大部分的用例都会先登录,那就需要把登录单独抽出来写个函数,其它用例全部的调用这个登录函数就行.但是登录 ...

  3. pytest_函数传参和firture传参数request

    前言为了提高代码的复用性,我们在写用例的时候,会用到函数,然后不同的用例去调用这个函数. 比如登录操作,大部分的用例都会先登录,那就需要把登录单独抽出来写个函数,其它用例全部的调用这个登陆函数就行. ...

  4. 函数传参和firture传参数request

    前言 为了提高代码的复用性,我们在写用例的时候,会用到函数,然后不同的用例去调用这个函数.比如登录操作,大部分的用例都会先登录,那就需要把登录单独抽出来写个函数,其它用例全部的调用这个登陆函数就行.但 ...

  5. pytest 12 函数传参和fixture传参数request

    前沿: 有的case,需要依赖于某些特定的case才可以执行,比如,登陆获取到的cookie,每次都需要带着他,为了确保是同一个用户,必须带着和登陆获取到的同一个cookies. 大部分的用例都会先登 ...

  6. Pytest系列(6) - conftest.py的详细讲解

    如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 什么是conftest.py 可以 ...

  7. Pytest系列(4) - fixture的详细使用

    如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 前面一篇讲了setup.te ...

  8. 深入理解javascript函数系列第二篇——函数参数

    × 目录 [1]arguments [2]内部属性 [3]函数重载[4]参数传递 前面的话 javascript函数的参数与大多数其他语言的函数的参数有所不同.函数不介意传递进来多少个参数,也不在乎传 ...

  9. jquery插件formValidator的ajaxValidator传参数问题

    最近在用formValidator插件,遇到一个问题.当我想用ajaxValidator的url传参数时,$("#tbName").val().document.getElemen ...

随机推荐

  1. Python 小技巧:如何实现操作系统兼容性打包?

    有一个这样的问题:现要用 setuptools 把一个项目打包成 whl 文件,然后 pip install 在 Windows/Linux 两种操作系统上,但是该项目中有一些依赖库只有 Window ...

  2. 测试必知必会系列- Linux常用命令 - mv

    21篇测试必备的Linux常用命令,每天敲一篇,每次敲三遍,每月一循环,全都可记住!! https://www.cnblogs.com/poloyy/category/1672457.html 移动当 ...

  3. [C++]请麻烦压一下定理的棺材板啦

    从去年还在竞赛的时候2/12的原博客里搬运来的 不得不说之前取名真的很艺术qwq 今天开始上的数论课,让头发以肉眼可见的速度掉落emmm 没关系我头发多我不怕啦啦啦QwQ 其中最令人头疼的就是那些人名 ...

  4. centOS6.5桌面版用不了中文输入法解决方案

    1:centos6.5中   系统->首选项->输入法中选择“使用iBus(推荐)”,点击首选输入法n遍,没有任何效果. 2.我也弄了很多种方式包括用 yum install " ...

  5. JavaScript 模式》读书笔记(3)— 字面量和构造函数2

    上一篇啊,我们聊了聊字面量对象和自定义构造函数.这一篇,我们继续,来聊聊new和数组字面量. 三.强制使用new的模式 要知道,构造函数,只是一个普通的函数,只不过它却是以new的方式调用.如果在调用 ...

  6. 学习scrapy爬虫框架的一些经验和教训

    首先python的scrapy框架很好,功能强大,使用起来也很方便,省去了很多造轮子的时间.在学习的过程中也碰到了一些问题,在这里希望能分享与大家分享,做一个参考 1.安装(pip延时响应问题) sc ...

  7. TensorFlow 多元线性回归【波士顿房价】

    1数据读取 1.1数据集解读 1.2引入包 %matplotlib notebook import tensorflow as tf import matplotlib.pyplot as plt i ...

  8. redis中setbit bitcount命令详解

    bitmap,位图,即是使用bit. redis字符串是一个字节序列. 1 Byte = 8 bit SETBIT key offset value 设置或者清空key的value(字符串)在offs ...

  9. deepin 系统 ssh,samba,redis,取消开机密码等相关配置

    ssh安装 sudo apt-get install openssh-server service ssh start ssh root 用户登入配置 安装完毕,运行命令"sudo vi / ...

  10. Prism 源码解读4-ViewModel注入

    介绍 介绍一个Prism的MVVM实现,主要介绍Prism如何在WPF上进行的一些封装,以实现MVVM.MVVM到底是什么呢?看一下这一幅经典的图 以前没有ViewModel这个概念,就是将Model ...