pytest文档42-fixture参数化params
前言
参数化是自动化测试里面必须掌握的一个知识点,用过 unittest 框架的小伙伴都知道使用 ddt 来实现测试用例的参数化。
pytest 测试用例里面对应的参数可以用 parametrize 实现,随着用例的增多,我们的需求也会越来越多,那么如何在 fixture 中使用参数呢?
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 a
fixture function.
The name of the fixture function can later be referenced to cause its
invocation ahead of running tests: test
modules or classes can use the ``pytest.mark.usefixtures(fixturename)``
marker.
Test functions can directly use fixture names as input
arguments in which case the fixture instance returned from the fixture
function 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 executed
as 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 multiple
invocations of the fixture function and all of the tests
using it.
The current parameter is available in ``request.param``.
:arg autouse: if True, the fixture func is activated for all tests that
can see it. If False (the default) then an explicit
reference is needed to activate the fixture.
:arg ids: list of string ids each corresponding to the params
so that they are part of the test id. If no ids are provided
they will be generated automatically from the params.
:arg name: the name of the fixture. This defaults to the name of the
decorated function. If a fixture is used in the same module in
which it is defined, the function name of the fixture will be
shadowed by the function arg that requests the fixture; one way
to 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 decoration
return 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
:arg params: an optional list of parameters which will cause multiple
invocations of the fixture function and all of the tests
using it.
The current parameter is available in ``request.param``.
fixture 之 params 使用示例
request 是pytest的内置 fixture ,主要用于传递参数
# test_fixture_params.py
import pytest
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/
# 测试数据,存放在list
user_data = ["user1", "user2"]
@pytest.fixture(scope="function", params=user_data)
def users(request):
'''注册用户参数化'''
return request.param
def test_register(users):
print("注册用户:%s"%users)
if __name__ == '__main__':
pytest.main(["-s", "test_fixture_params"])
运行结果
>pytest test_fixture_params.py -s
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-4.5.0, py-1.5.4, pluggy-0.13.1
rootdir: D:\soft\demo
plugins: allure-pytest-2.8.6
collected 2 items
test_fixture_params.py 注册用户:user1
.注册用户:user2
.
========================== 2 passed in 0.02 seconds ===========================
前置与后置
如果每次注册用户之前,需先在前置操作里面清理用户注册表的数据,可以执行SQL,传不同用户的参数
# test_fixture_params.py
import pytest
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/
def delete_sql(user):
'''这里执行SQL'''
sql = "delete from auth_user WHERE username = '%s';"%user
print("执行的sql:%s"%sql)
# 调用执行SQL的封装函数
# 测试数据,存放在list
user_data = ["user1", "user2"]
@pytest.fixture(scope="function", params=user_data)
def users(request):
'''注册用户参数化'''
# 前置操作
delete_sql(request.param)
yield request.param
# # 后置操作
# delete_sql(request.param)
def test_register(users):
print("注册用户:%s"%users)
if __name__ == '__main__':
pytest.main(["-s", "test_fixture_params.py"])
运行结果
collected 2 items
test_fixture_params.py 执行的sql:delete from auth_user WHERE username = 'user1';
注册用户:user1
.执行的sql:delete from auth_user WHERE username = 'user2';
注册用户:user2
.
========================== 2 passed in 0.06 seconds ===========================
后置操作可以写到 yield 后面,参考上面的格式。
pytest文档42-fixture参数化params的更多相关文章
- pytest文档7-pytest-html生成html报告
前言 pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.github上源码地址[https://github. ...
- pytest文档3-pycharm运行pytest
前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻 ...
- pytest文档51-内置fixture之cache使用
前言 pytest 运行完用例之后会生成一个 .pytest_cache 的缓存文件夹,用于记录用例的ids和上一次失败的用例. 方便我们在运行用例的时候加上--lf 和 --ff 参数,快速运行上一 ...
- pytest文档1-环境准备与入门
前言 首先说下为什么要学pytest,在此之前相信大家已经掌握了python里面的unittest单元测试框架,那再学一个框架肯定是需要学习时间成本的. 刚开始我的内心是拒绝的,我想我用unittes ...
- pytest文档43-元数据使用(pytest-metadata)
前言 什么是元数据?元数据是关于数据的描述,存储着关于数据的信息,为人们更方便地检索信息提供了帮助. pytest 框架里面的元数据可以使用 pytest-metadata 插件实现.文档地址http ...
- pytest文档21-pytest-html报告优化(nodeid中文显示[\u6350\u52a9\u6211\u4eec]问题解决)
前言 pytest-html报告中当用到参数化时候,获取用例的nodeid里面有中文时候,会显示[\u6350\u52a9\u6211\u4eec]这种编码(再次声明,这个不叫乱码,这是unicode ...
- pytest文档19-doctest测试框架
前言 doctest从字面意思上看,那就是文档测试.doctest是python里面自带的一个模块,它实际上是单元测试的一种. 官方解释:doctest 模块会搜索那些看起来像交互式会话的 Pytho ...
- pytest文档56-插件打包上传到 pypi 库
前言 pytest 的插件完成之后,可以上传到 github,方便其他小伙伴通过 pip 源码安装.如果我们想通过 pip install packages 这种方式安装的话,需上传到 pypi 仓库 ...
- pytest文档55-plugins插件开发
前言 前面一篇已经学会了使用hook函数改变pytest运行的结果,代码写在conftest.py文件,实际上就是本地的插件了. 当有一天你公司的小伙伴觉得你写的还不错,或者更多的小伙伴想要你这个功能 ...
随机推荐
- python语法,基础
1.用缩进表达代码逻辑:推荐缩进4个空格 2.注释和续行与shell一样 在pycharm中,按ctrl + / 可以快速添加/取消注释 3.多个语句书写在同一行,可以使用分号进行分割.但不推荐 输出 ...
- Django 仿ajax传递数据(Django十)
之前用form表单传递数据,没有遇到任何问题 具体见:https://blog.csdn.net/qq_38175040/article/details/104867747 然后现在我想用ajax传递 ...
- docker下jira数据备份和还原注意路径
重点:jira页面上的备份与恢复页面的路径,跟我实际路径是有出入的,如果找不到就用find去搜下备份文件就知道怎么操作了 1.登录jira后,右上角的系统——左侧的导入与导出——选择里面的备份与恢复 ...
- linux系统的默认用户
1.可以通过cat /etc/passwd |cut -f1 -d: 可以提取/etc/passwd文件的第一个字段 2.也可以通过vipw进入vi编辑器来查看该文件每一行的第一个字段 linux ...
- [剑指Offer]30-包含min函数的栈
题目 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). 题解 辅助栈记录当前最小值. 代码 import java.util.Stack; pub ...
- vue | 基于vue的城市选择器和搜索城市对应的小区
城市选择器应该是比较常用的一个组件,用户可以去选择自己的城市,选择城市后返回,又根据自己选择的城市搜索小区. 功能展示 这是选择结果 这是选择城市 这是搜索小区 这是搜索小区接口,key为城市名字,i ...
- vue自适应布局的n中方法
在index.html中 head标签中 <script> //通过window.screen.width获取屏幕的宽度 var offWidth = window.screen.widt ...
- 【C#上位机必看】你们要的Iot物联网项目来了
新阁教育喜科堂不得不说,工业圈是个比较奇怪的圈子,各种各样的项目需求都有,有的人就希望价格低,功能实现即可,有的人又不在乎价格,就要界面好看.最近有一个小伙伴又提出了这样的需求,用最简单的方式,最低的 ...
- MVC设计模式-笔记1
MVC不仅仅是一个设计模式,它应该说是一种软件开发架构模式,它包含了很多的设计模式,最为密切是以下三种模式: 1.Observer观察者模式 2.Composite组合模式 3.Strategy策略模 ...
- solr综合案例
1. 综合案例 1.1. 需求 使用Solr实现电商网站中商品信息搜索功能,可以根据关键字.分类.价格搜索商品信息,也可以根据价格进行排序,并且实现分页功能. 界面如下: 1.2分析 开发人员需要的 ...