Pytest fixture参数化params

unittest使用ddt来实现测试用例参数化、或parameterized实现测试用例参数化,pytest测试用例里面对应的参数可以用 parametrize 实现参数化,今天我们来了解下fixture参数化params

fixture的参数可以解决大量重复代码工作,比如数据库的连接、查询、关闭等.同样可以使用参数化来测试多条数据用例。

fixture源码:

传入参数scope,params,autouse,ids,name

def fixture(scope="function", params=None, autouse=False, ids=None, name=None):"""Decorator to mark a fixture factory function.This decorator can be used, with or without parameters, to define afixture function.The name of the fixture function can later be referenced to cause itsinvocation ahead of running tests: testmodules or classes can use the ``pytest.mark.usefixtures(fixturename)``marker.Test functions can directly use fixture names as inputarguments in which case the fixture instance returned from the fixturefunction will be injected.Fixtures can provide their values to test functions using ``return`` or ``yield``statements. When using ``yield`` the code block after the ``yield`` statement is executedas teardown code regardless of the test outcome, and must yield exactly once.:arg scope: the scope for which this fixture is shared, one of``"function"`` (default), ``"class"``, ``"module"``,``"package"`` or ``"session"``.``"package"`` is considered **experimental** at this time.:arg params: an optional list of parameters which will cause multipleinvocations of the fixture function and all of the testsusing it.The current parameter is available in ``request.param``.:arg autouse: if True, the fixture func is activated for all tests thatcan see it.  If False (the default) then an explicitreference is needed to activate the fixture.:arg ids: list of string ids each corresponding to the paramsso that they are part of the test id. If no ids are providedthey will be generated automatically from the params.:arg name: the name of the fixture. This defaults to the name of thedecorated function. If a fixture is used in the same module inwhich it is defined, the function name of the fixture will beshadowed by the function arg that requests the fixture; one wayto resolve this is to name the decorated function``fixture_<fixturename>`` and then use``@pytest.fixture(name='<fixturename>')``."""if callable(scope) and params is None and autouse is False:# direct decorationreturn FixtureFunctionMarker("function", params, autouse, name=name)(scope)if params is not None and not isinstance(params, (list, tuple)):params = list(params)return FixtureFunctionMarker(scope, params, autouse, ids=ids, name=name)

params 参数:一个可选的参数列表,它将导致多次调用fixture函数和使用它的所有测试,获取当前参数可以使用request.param,request 是pytest的内置 fixture ,主要用于传递参数

1、获取账号密码案例:

import pytestdata = [("username1", "password1"), ("username2", "password2")]
# data = (("username1", "password1"), ("username2", "password2"))
# data = [["username1", "password1"], ["username2", "password2"]]@pytest.fixture(scope = "function", params = data)
def get_data(request):print(request.param)return request.paramdef test_login(get_data):print("账号:%s"%get_data[0],"密码:%s"%get_data[1])if __name__ == '__main__':pytest.main(["-s", "test_C_01.py"])test_C_01.py ('username1', 'password1')
账号:username1 密码:password1
.('username2', 'password2')
账号:username2 密码:password2
.============================== 2 passed in 0.08s ==============================Process finished with exit code 0

2、前置准备后置清理案例:

import pytest
# 封装删除用户sql
def delete_user(user):sql = "delete from user where mobile = '%s'"%userprint("删除用户sql:%s"%sql)
# 测试数据
mobile_data = ["18200000000", "18300000000"]@pytest.fixture(scope="function", params=mobile_data)
def users(request):'''注册用户参数化'''# 前置操作delete_user(request.param)yield request.param# 后置操作delete_user(request.param)def test_register(users):print("注册用户:%s"%users)if __name__ == '__main__':pytest.main(["-s", "test_C_01.py"])test_C_01.py 删除用户sql:delete from user where mobile = '18200000000'
注册用户:18200000000
.删除用户sql:delete from user where mobile = '18200000000'
删除用户sql:delete from user where mobile = '18300000000'
注册用户:18300000000
.删除用户sql:delete from user where mobile = '18300000000'============================== 2 passed in 0.12s ==============================Process finished with exit code 0

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/568244.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python3中选择文件对话框的格式打开和保存图片

tkinter.filedialog.asksaveasfilename() # 选择以什么文件名保存&#xff0c;返回文件名 tkinter.filedialog.askopenfilename() # 选择打开什么文件&#xff0c;返回文件名有时候我们想要以选择文件对话框的格式打开一张图片或者保存图片&#xff0c;我在尝试之后把我的…

Python测试开发django1.简介

Django是一种基于Python开发的开源的高级Web应用框架&#xff0c;使用Django&#xff0c;使你能够以最小的代价构建和维护高质量的Web应用。Django 本身基于 MVC 模型&#xff0c;即 Model&#xff08;模型&#xff09; View&#xff08;视图&#xff09; Controller&#xff0…

Python测试开发django2.环境部署项目创建

Django是由Python编写的Web框架&#xff0c;依赖Python环境&#xff0c;所以需要提前安装好Python环境。建议安装最新版本的Python3&#xff0c;Python 下载地址&#xff1a;https://www.python.org/downloads/1、Django官网https://www.djangoproject.com/2、Django版本Django…

excel如何输入毫秒级时间

1、选择单元格或单元格区域&#xff1b; 2、右击-设置单元格格式&#xff08;或按ctrl1&#xff09;&#xff1b; 3、数字选项卡-自定义-h:mm:ss.000 4、单元格就可以输入精度为千分之一秒的时间&#xff0c;如&#xff1a;20:15:21.451 参考自https://zhidao.baidu.com/questi…

Python测试开发django3.视图和URL配置

1、Django path&#xff08;&#xff09;方法Django路由系统中最重要的path()方法可以接收4个参数&#xff0c;其中2个是必须的&#xff1a;route和view&#xff0c;以及2个可选的参数&#xff1a;kwargs和namedef path(route, view, kwargsNone, nameNone):return re_path(rou…

Python测试开发django4.templates模板配置

【上一篇】我们讲了Python测试开发django3.视图和URL配置今天详细介绍下 Django 模板的应用&#xff0c;模板是一个文本&#xff0c;用于分离文档的表现形式和内容。我们已经知道创建项目用django-admin startproject helloworld&#xff0c;一个项目下可以有多个应用&#xff…

QImage QPixmap Mat区别

cvMat可以通过自定义函数转换为QImage Qimage通过fromImage函数 可以转换为QPixmap 绘图设备是指继承QPainterDevice的子类。Qt一共提供了四个这样的类&#xff0c;分别是QPixmap、QBitmap、QImage和 QPicture。其中&#xff0c;QPixmap专门为图像在屏幕上的显示做了优化&…

Python测试开发django5.templates模板变量传参

上一篇&#xff0c;我们学习了Python测试开发django4.templates模板配置templates模板中html文件是一个静态页面&#xff0c;写四的&#xff0c;如果有时我们想动态的传入一些不同的参数&#xff0c;想实现在一个固定的html样式&#xff0c;这就可以用django的模板变量传参来解…

git入门一

1、下载git并安装 &#xff08;github网站中 start 收藏 follow 关注 watch &#xff08;关注项进度&#xff09;查看进度&#xff09; 2、启动git。一般用git bash 3、改变git bash界面 4、在本地建立git bash仓库 到本地文件夹下&#xff0c;右击建立 git bash here。 5、…

Python测试开发django5.urls.py参数name与a标签的引用

上一篇&#xff0c;我们学习了Python测试开发django5.templates模板变量传参如果我们有2个页面home.html和demo.html&#xff0c;两个页面是独立的没有关系的&#xff0c;现在需要从home.html页&#xff0c;点某个超链按钮&#xff0c;跳转到demo.html页&#xff0c;home.html页…

git入门二

exit 退出git bash 命令窗口 1、刚安装成功界面&#xff1a; 2、查看配置信息&#xff1a; 3、配置用户名和邮箱 此时再看git config --list。已经有用户名和邮箱的信息 4、把当前目录初始化为仓库并提交 5、将远程和本地连接 &#xff08;1&#xff09;在https//github.com 上…

opencv1-加载、修改、保存图像

我的实践&#xff1a; #include<opencv2\opencv.hpp> #include<iostream> using namespace cv; using namespace std; int main() {Mat src imread("E:\\vs2015\\opencvstudy\\2.jpg", 1);if (src.empty()){cout << "could not load the i…

接口自动化实战设计思路,想法及疑问(一)

各位粉丝朋友们大家好&#xff0c;最近在学习研究接口自动化测试时&#xff0c;在设计思路和实践过程中&#xff0c;碰到了很多问题&#xff0c;再不断的优化和调整&#xff0c;这过程中产生了很多疑问和不解&#xff0c;并与很多测试的朋友进行交流想法&#xff0c;但是各自想…

2019年下半年《软件评测师》下午试卷及答案

一、阅读下列C程序&#xff0c;回答问题1至问题3。问题&#xff1a;1.1 (6分)请针对上述C程序给出满足100%DC (判定覆盖)所需的逻辑条件。buf_c[i]<7||buf_c[i]>14&#xff1b;i>32; buf_len>512&#xff1b;buf_len<512 buf_len0&#xff1b;buf_len!0 i<…

2018年下半年《软件评测师》下午试卷及答案

&#xff08;共15分&#xff09;一、阅读下列C程序&#xff0c;回答问题1至问题3,将解答填入答题纸的对应栏内。问题&#xff1a;1.1 &#xff08;3分&#xff09;请针对上述C程序给出满足100%DC&#xff08;判定覆盖&#xff09;所需的逻辑条件。本问题考查白盒测试用例设计…

opencv3-Mat对象

我的实践&#xff1a; #include<opencv2\opencv.hpp> #include<iostream> #include<math.h> using namespace cv; using namespace std; int main() {//加载图像Mat src imread("E:\\vs2015\\opencvstudy\\2.jpg", 1);if (!src.data){cout <&…

2017年下半年《软件评测师》下午试卷及答案

一、【C程序】问题&#xff1a;1.1 &#xff08;3分&#xff09;请针对上述C程序给出满足100%DC&#xff08;判定覆盖&#xff09;所需的逻辑条件。本题考查白盒测试法及应用。本问题考查白盒测试用例设计方法中的判定覆盖法。判定覆盖指设计足够的测试用例&#xff0c;使得被…

opencv4-图像操作

这里Vex3f 也可以是Vec3b #include<opencv2\opencv.hpp> #include<iostream> #include<math.h> using namespace cv; using namespace std;//读写图像&#xff0c; //读写像素&#xff0c; //修改像素值 int main() {Mat src imread("E:\\vs2015\\open…

2016年下半年《软件评测师》下午试卷及答案

一、阅读下列C程序&#xff0c;回答问题1至问题3&#xff0c;将解答填入答题纸的对应栏内。【C程序】问题&#xff1a;1.1 请针对上述C程序给出满足100%DC&#xff08;判定覆盖&#xff09;所需的逻辑条件本问题考查白盒测试用例设计方法中的判定覆盖法。判定覆盖指设计足够的…

opencv5-图像混合

代表一幅图像。代表权重&#xff0c;取值范围为0~1。代表另一幅图像 对图像的每一个像素进行此操作。得到混合后图像 我的实践&#xff1a; #include<opencv2\opencv.hpp> #include<iostream> #include<math.h> using namespace cv; using namespace std; …