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 ...
随机推荐
- 二进制安装docker-20.10.9
二进制包下载地址:https://download.docker.com/linux/static/stable/x86_64/ #解压tar xf docker-20.10.9.tgz #拷贝二进 ...
- hexo部署和优化记录
title: hexo部署和优化记录 date: 2020-06-14 09:00:03 前端 tags: hexo summary: Repository_Pages使用.Github仓库打造网页群 ...
- ubuntu配置docker全局系统代理
(1)添加用户到docker用户组 sudo groupadd docker sudo gpasswd -a kang docker (注销系统当前用户,再次登录) #检查是否添加到组 cat /et ...
- mysql中的InnoDB和MyISAM
InnoDB:支持事务,支持表级锁,行级锁,支持外键,是非聚集索引. MyISAM:不支持事务,支持表级锁,不支持外键,聚集索引. 聚集索引和非聚集索引的区别:InnoDB是聚集索引,使用B+Tree ...
- cnblogs.com/linycat
核心技术 Java MySQL SSM SpringBoot MyBatisPlus Redis RabbitMQ Git Google ChatGPT 分布式架构 分布式会话 单点登录 分布式锁 读 ...
- WPF 使用Path(自定义控件,圆形进度条)
原文:https://www.cnblogs.com/tsliwei/p/5609035.html 原文链接:https://blog.csdn.net/johnsuna/article/detail ...
- ASP.NET中maxRequestLength和maxAllowedContentLength的区别;上传大文件设置IIS7文件上传的最大大小
https://blog.csdn.net/qq_23663693/article/details/89920039 maxRequestLength表示ASP支持的最大请求大小,而maxAllowe ...
- webapi fromurl frombody
https://blog.csdn.net/QiGary/article/details/113979877 在做后台api接口时,常常涉及到Http方法访问问题,其中最基础也是最核心的就是传参问题. ...
- sql-log
使用插件必须 先设置打印sql日志级别 debug 不然打不出来 SQL Params Setter插件 Ctrl+V mybaties log
- superset2.0.0 支持MaxCompute 时间颗粒
编译的时候需要在superset/superset/db_engine_specs以下路径增加一个 odps.py的文件 # Licensed to the Apache Software Found ...