Pytest Fixture(三)
name: name参数表示可以对fixture的名称进行重命名:
注意:通过name重命名后,继续使用以前的名字调用会报错。
import pytest @pytest.fixture(name='anjing')
def login():
print('\n登录操作')
yield
print('\n退出登录!') class TestLogin:
def test_01(self, anjing):
print('---用例01---') def test_02(self):
print('---用例02---') if __name__ == '__main__':
pytest.main(['-vs'])
params:参数化
假设现在有一批 API 需要测试对不同数据库的支持情况(对所有数据库进行相同操作),最简单的方法就是针对每个数据库编写一个测试用例,但这包含大量重复代码,如数据库的连接、关闭,查询等。
使用固件抽离出数据库的通用操作,每个 API 都能复用这些数据库固件,同时可维护性也得到提升。
import pytest @pytest.fixture(params=[
('redis', '6379'),
('elasticsearch', '9200')
])
def param(request):
print(f"数据库的配置:{request.param}")
return request.param @pytest.fixture(autouse=True)
def db(param):
print(f'数据库连接了{param}')
yield
print(f'数据库关闭了{param}') def test_api():
assert 1 == 1
执行结果

import pytest data = ['anjing', 'test', 'admin'] @pytest.fixture(params=data, ids=['user=anjing', 'user=test', 'user=admin'])
def login(request):
print('登录功能')
yield request.param
print('退出登录') class TestCase:
def test_01(self, login):
print('---用例01---')
print(f'登录的用户名{login}') def test_02(self):
print('---用例02---') if __name__ == '__main__':
pytest.main(['-vs'])
Pytest Fixture(三)的更多相关文章
- Pytest测试框架(三):pytest fixture 用法
xUnit style 结构的 fixture用于初始化测试函数, pytest fixture是对传统的 xUnit 架构的setup/teardown功能的改进.pytest fixture为测试 ...
- Pytest fixture及conftest详解
前言 fixture是在测试函数运行前后,由pytest执行的外壳函数.fixture中的代码可以定制,满足多变的测试需求,包括定义传入测试中的数据集.配置测试前系统的初始状态.为批量测试提供数据源等 ...
- pytest.fixture和普通函数调用
普通函数嗲用def one(): a="aaaaaaaaaaa" return a def test_one(): s=one() print (s) test_one() pyt ...
- pytest 用 @pytest.mark.usefixtures("fixtureName")或@pytest.fixture(scope="function", autouse=True)装饰,实现类似setup和TearDown的功能
conftest.py import pytest @pytest.fixture(scope="class") def class_auto(): print("&qu ...
- pytest fixture 利用 params参数实现用例集合
@pytest.fixture有一个params参数,接受一个列表,列表中每个数据都可以作为用例的输入.也就说有多少数据,就会形成多少用例.如下面例子,就形成3条用例 test_parametrizi ...
- pytest fixture中scope试验,包含function、module、class、session、package
上图是试验的目录结构 conftest.py:存放pytest fixture的文件 import uuid import pytest @pytest.fixture(scope="mod ...
- python单元测试框架pytest——fixture函数(类似unitest的setup和teardown)
pytest的setup和teardown函数(曾被一家云计算面试官问到过). pytest提供了fixture函数用以在测试执行前和执行后进行必要的准备和清理工作.与python自带的unitest ...
- 学习-Pytest(三)setup/teardown
1. 用例运行级别 模块级(setup_module/teardown_module)开始于模块始末,全局的 函数级(setup_function/teardown_function)只对函数用例生效 ...
- pytest封神之路第三步 精通fixture
首先放一句"狠话". 如果你不会fixture,那么你最好别说自己会pytest. (只是为了烘托主题哈,手上的砖头可以放下了,手动滑稽) fixture是什么 看看源码 def ...
- 『德不孤』Pytest框架 — 13、Pytest中Fixture装饰器(三)
目录 9.ids参数说明 10.name参数说明 11.scope参数说明 (1)scope="function" (2)scope="class" (3)sc ...
随机推荐
- 遍历dom节点
function selectOnchang(obj) { var selectedValue = obj.selectedIndex if (selectedValue == 0) { obj.pa ...
- [iOS]Universal Link
从零开始的操作流程在后面,这里把几个坑先挪到前面来 便于查看: ️ apple-app-site-association 只会在APP第一次启动的时候请求一次,因此文件的任何更新的验证都需要APP重新 ...
- linux mint 中文支持
sudo apt-get install localessudo dpkg-reconfigure locales 设置zh_CN.UTF-8
- [ZZH]第一篇博客
time: 2022/3/29 20:24 my first blog
- 使用EmguCV3.0.0调用摄像头并设置帧率
此教程使用emgu3.0.0,其他版本兼容性未知. 转载请注明出处: http://www.cnblogs.com/zaiyuzhong/p/open-camera-with-emgu3_0_0.ht ...
- 插入排序C语言
1 #include "stdio.h" 2 #include "stdlib.h" 3 typedef int ElemType; 4 //直接插入排序 5 ...
- Charles抓包工具详解
学习Charles能做什么: 能够用charles分析前后端问题 能够使用charles模拟弱网测试环境 能够使用charles断点构建异常的测试环境 Charles 简介 1.Charles是什么? ...
- css背景模糊化
处理背景模糊化 在css中,可以利用filter属性和blur()函数实现高斯模糊效果,filter属性用于设置图片元素的可视效果,配合blur()函数使用可给图片元素添加高斯模糊效果,语法为&quo ...
- 【笔记】Win7连接公司内网无法打开网页
win7尝试使用VPN连接公司内网发现无法打开单点登录网页 DNS解析没问题 服务器也能ping通 查了半天才发现是MTU的问题 win7的默认值1400 公司的华为设备设置的比这个值小 要根据具体的 ...
- 计算机科学导论-第三版-学习笔记-chapter2-数字系统
原本看答案的网站被上保护了,我没账号看不了,开摆. 猜测是那边的学生做作业用chatGPT,部分教师觉得不行,禁止使用的同时把答案都上锁了. 也可能是单纯因为我没报课就没账号. 复习题 1.定义一个数 ...