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

Pytest fixture参数化params相关推荐

  1. 【pytest】(十)fixture参数化-巧用params和ids优雅的创建测试数据

    我们都知道参数化. 比如我要测试一个查询接口/test/get_goods_list,这个接口可以查询到商品的信息. 在请求中,我可以根据请参数goods_status的不同传值,可以查询到对应状态的 ...

  2. pytest.5.参数化的Fixture

    From: http://www.testclass.net/pytest/parametrize_fixture/ 背景 继续上一节的测试需求,在上一节里,任何1条测试数据导致断言不通过后测试用例就 ...

  3. pytest之fixture参数化

    背景 本文总结fixture参数化 说明 pytest除了支持基本的测试用例参数化,还支持fixture参数化.当然,fixture参数化的过程与测试用例参数化有点点区别. fixture的参数化涉及 ...

  4. pytest实战--参数化parametrize+前置fixture

    文章目录 参数化 parametrize 适用场景 一个典型的例子 前置fixture+参数化 适用场景 一个典型的例子 示例2 个人疑问? pytest既可以用来做单元测试,也可以用来做自动化接口测 ...

  5. Python pytest框架之@pytest.fixture()和conftest详解

    一.fixture简介 学pytest就不得不说fixture,fixture是pytest的精髓所在,类似unittest中setup/teardown这种前后置东西.但是比它们要强大.灵活很多,它 ...

  6. 【pytest】(二) pytest中的fixture (1) : fixture和fixture API —@pytest.fixture()的简单说明

    目录 1. fixture介绍 1.1 什么是fixture 1.2 与xUnit风格的setup和teardown的对比 1.3 fixture运行报错后,pytest的处理方式 2.fixture ...

  7. Pytest Fixture详解

    前言 在做自动化的过程中,编写用例时候需要用到用例的前置和用例的后置,其中pytest中有setup_class和teardown_class可以帮助我们完成这些,但是不够完善而且灵活性不够强.举个简 ...

  8. Pytest fixture及conftest详解

    前言 fixture是在测试函数运行前后,由pytest执行的外壳函数.fixture中的代码可以定制,满足多变的测试需求,包括定义传入测试中的数据集.配置测试前系统的初始状态.为批量测试提供数据源等 ...

  9. java fixture_10、fixture参数化

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

最新文章

  1. 【Laravel-海贼王系列】第九章, Events 功能解析
  2. Python使用matplotlib绘制数据去重前后的柱状图对比图(在同一个图中显示去重操作之后同一数据集的变化情况)
  3. c++函数模板_高考数学解答题得分模板——三角函数与解三角形
  4. 取消chrome下input和textarea的聚焦边框
  5. 拍照时不会摆Pose怎么办?
  6. pandas to_dict 的用法
  7. marquee文字起始位置_PS修图改字无痕扫描件复印件截图文字英文日期修改:制作漂亮红色丝绸文字图片的PS教程...
  8. Linux系统中的load average
  9. mysql cmd 实时监控_MySQL实时监控工具orztop的使用介绍
  10. sql查询出的字段切割_SPL 简化 SQL 案例详解:多层固定分组
  11. 如何理解Linux shell中的“21”?
  12. 全网首发:制作LINUX安装软件包,要处理哪些系统目录和文件(1)
  13. Android DCIM相册保存
  14. 将bilibili里面的缓存视频保存到电脑
  15. 计算机软硬件的组成及主要技术指标,计算机软硬件系统的组成及主要技术指标...
  16. 一键开关电路设计(一)
  17. 扬帆际海—shopee跨境店和本土店谁更有优势?
  18. 【QT项目——视频播放器——解码】5.1decoder-5.10音频重采样
  19. 将时间戳“年月日 时分秒”格式转换成“年月日”格式
  20. C语言中的输出99乘法表4种方法

热门文章

  1. numpy 矩阵与向量相乘_高能!8段代码演示Numpy数据运算的神操作
  2. matlab数据处理 书,matlab数据处理记录
  3. Queue —— JUC 的豪华队列组件
  4. php查找以xx结尾的的字符串单词,Javascript中查找不以XX字符结尾的单词示例代码_javascript技巧...
  5. php验证旧密码,PHP最佳实践之过滤、验证、转义和密码
  6. (kruskal算法复习+模板)Eddy's picture
  7. (priority_queue)自定义优先级
  8. php求链表中位数,先给伸手党的php链表遍历求和
  9. java strcpy,详解C语言中strcpy()函数与strncpy()函数的使用
  10. python3dijkstra_python3 实现Dijkstra(迪杰斯特拉)最短路径算法