Pytest系列(10) - firture 传参数 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条
Pytest系列(10) - firture 传参数 request的详细使用的更多相关文章
- pytest文档14-函数传参和firture传参数request
前言 为了提高代码的复用性,我们在写用例的时候,会用到函数,然后不同的用例去调用这个函数. 比如登录操作,大部分的用例都会先登录,那就需要把登录单独抽出来写个函数,其它用例全部的调用这个登陆函数就行. ...
- pytest十一:函数传参和 firture 传参数 request
为了提高代码的复用性,我们在写用例的时候,会用到函数,然后不同的用例去调用这个函数.比如登录操作,大部分的用例都会先登录,那就需要把登录单独抽出来写个函数,其它用例全部的调用这个登录函数就行.但是登录 ...
- pytest_函数传参和firture传参数request
前言为了提高代码的复用性,我们在写用例的时候,会用到函数,然后不同的用例去调用这个函数. 比如登录操作,大部分的用例都会先登录,那就需要把登录单独抽出来写个函数,其它用例全部的调用这个登陆函数就行. ...
- 函数传参和firture传参数request
前言 为了提高代码的复用性,我们在写用例的时候,会用到函数,然后不同的用例去调用这个函数.比如登录操作,大部分的用例都会先登录,那就需要把登录单独抽出来写个函数,其它用例全部的调用这个登陆函数就行.但 ...
- pytest 12 函数传参和fixture传参数request
前沿: 有的case,需要依赖于某些特定的case才可以执行,比如,登陆获取到的cookie,每次都需要带着他,为了确保是同一个用户,必须带着和登陆获取到的同一个cookies. 大部分的用例都会先登 ...
- Pytest系列(6) - conftest.py的详细讲解
如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 什么是conftest.py 可以 ...
- Pytest系列(4) - fixture的详细使用
如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 前面一篇讲了setup.te ...
- 深入理解javascript函数系列第二篇——函数参数
× 目录 [1]arguments [2]内部属性 [3]函数重载[4]参数传递 前面的话 javascript函数的参数与大多数其他语言的函数的参数有所不同.函数不介意传递进来多少个参数,也不在乎传 ...
- jquery插件formValidator的ajaxValidator传参数问题
最近在用formValidator插件,遇到一个问题.当我想用ajaxValidator的url传参数时,$("#tbName").val().document.getElemen ...
随机推荐
- Python中使用subprocess模块远程执行命令
使用subprocess模块执行远程命令 服务端代码 1 import socket 2 import subprocess 3 4 sh_server = socket.socket() 5 sh_ ...
- 环境变量之classpath配置和临时配置
前言 本篇文章介绍classpath环境变量的配置和一个环境变量配置的技巧:临时配置path环境变量. 正文 classpath环境变量配置 在上完了"Hello World"这堂 ...
- js 运动的应用 新浪微博
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Git&sourceTree软件安装、使用说明及遇到问题解决
一.软件版本 1.Git版本为1.9.5 2.Source版本为1.5.2 二.软件安装步骤 1.Git安装步骤 1)双击Git安装文件进入下图界面,单击Next 2)继续Next 3)进入Selec ...
- C 和 C++语言中的内存拷贝函数memcpy()
memcpy指的是C和C++使用的内存拷贝函数 函数原型为void *memcpy(void *destin, void *source, unsigned n): 函数的功能是从源内存地址的起始位置 ...
- 【TIJ4】第三章全部习题
题目都相当简单没啥说的直接放代码就行了... 3.1 package ex0301; //[3.1]使用“简短的”和正常的打印语句来写一个程序 import static java.lang.Syst ...
- Asp.Net Core 3.1 获取不到Post、Put请求的内容 System.NotSupportedException Specified method is not supported
# 问题 是这样的,我.net core 2.1的项目,读取.获取Post请求内容的一段代码,大概这样: [HttpPost] public async Task<IActionResult& ...
- Journal of Proteome Research | Quantitative Subcellular Proteomics of the Orbitofrontal Cortex of Schizophrenia Patients (精神分裂症病人眶额叶皮层亚细胞结构的定量蛋白质组学研究)(解读人:王聚)
期刊名:Journal of Proteome Research 发表时间:(2019年10月) IF:3.78 单位: 里约热内卢联邦大学 坎皮纳斯州立大学 坎皮纳斯州立大学神经生物学中心 卡拉博大 ...
- Mybatis三剑客介绍
1.MyBatis generator 利用mybatis-generator自动生成代码 下载地址: https://download.csdn.net/download/qq_36625806/ ...
- body体里面传///貌似jmeter不支持
原因是:当接口请求参数含\\\时请求到服务器端是\\,少了一个\导致参数不合法报错:求助一个大神说是 因为如标题 : 解决如上图所示,完美解决