pytest fixture中scope试验,包含function、module、class、session、package

上图是试验的目录结构
conftest.py:存放pytest fixture的文件
import uuid
import pytest @pytest.fixture(scope="module")
def test_module():
return "module"+str(uuid.uuid4()) @pytest.fixture(scope="class")
def test_class():
return "class"+str(uuid.uuid4()) @pytest.fixture(scope="function")
def test_function():
return "function"+str(uuid.uuid4()) @pytest.fixture(scope="session")
def test_session():
return "session"+str(uuid.uuid4()) @pytest.fixture(scope="package")
def test_package():
return "package"+str(uuid.uuid4())
test_class.py:类测试文件
class Test_for_pytest_scope(object):
def test_case1(self,test_module,test_class,test_function,test_session,test_package):
print("testcase/test_class.py-test_case1||" + test_function)
print("testcase/test_class.py-test_case1||"+test_module)
print("testcase/test_class.py-test_case1||"+test_class)
print("testcase/test_class.py-test_case1||" + test_session)
print("testcase/test_class.py-test_case1||" + test_package) def test_case2(self,test_module,test_class,test_function,test_session,test_package):
print("testcase/test_class.py-test_case2||" + test_function)
print("testcase/test_class.py-test_case2||"+test_module)
print("testcase/test_class.py-test_case2||"+test_class)
print("testcase/test_class.py-test_case2||" + test_session)
print("testcase/test_class.py-test_case2||" + test_package)
其它测试文件都是打印fixture的返回信息
下图是打印结果

结论:
package的试验结果和预期有些出入,其它的作用范围大小关系为 function<class<module<session
| function | 每个方法和函数执行前都会重新调用一些fixture,得到一个新的uuid |
| class | 一个文件(module)内,class内部方法共享数据,函数不共享数据 |
| module | 一个文件就是一个module,一个module内只执行一次fixture,且module内共享数据 |
| package | uuid都一致,和理解上有些出入,以为不同package会重新调用一次fixture |
| session |
一次执行都一致 |
补充:翻看官方文档,发现package还处于试验阶段。

pytest fixture中scope试验,包含function、module、class、session、package的更多相关文章
- 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:通过scope控制fixture的作用范围
一.fixture里面有个参数scope,通过scope可以控制fixture的作用范围,根据作用范围大小划分:session>module>class>function,具体作用范 ...
- jsp中iframe所包含的页面调用父页面的function方法
a.jsp中写了注册的Ext窗体loginWindow,也用iframe包含了另一个b.jsp,当在b.jsp中也有点击注册的链接,这时需要用到调用a.jsp中的loginWindow,因为如果你重新 ...
- abap function module中的异常处理
1: 定义一个有异常抛出的function module. (zfm_moudle6), 该函数中有符合exceptions中的异常,将会自动将exceptions中的异常抛出. FUNCTION ...
- 在function module 中向数据库插入数据
http://www.sapjx.com/abap-function-module.html 1: 应该在function module 中向数据库插入数据
- Pytest fixture及conftest详解
前言 fixture是在测试函数运行前后,由pytest执行的外壳函数.fixture中的代码可以定制,满足多变的测试需求,包括定义传入测试中的数据集.配置测试前系统的初始状态.为批量测试提供数据源等 ...
- pytest fixture 利用 params参数实现用例集合
@pytest.fixture有一个params参数,接受一个列表,列表中每个数据都可以作为用例的输入.也就说有多少数据,就会形成多少用例.如下面例子,就形成3条用例 test_parametrizi ...
- Pytest测试框架(三):pytest fixture 用法
xUnit style 结构的 fixture用于初始化测试函数, pytest fixture是对传统的 xUnit 架构的setup/teardown功能的改进.pytest fixture为测试 ...
- pytest--fixture基本使用(主要用来进行测试环境的初始化和清理,fixture中的params参数还可以用来进行参数化)
fixture fixture修饰器来标记固定的工厂函数,在其他函数,模块,类或整个工程调用它时会被激活并优先执行,通常会被用于完成预置处理和重复操作. 方法: fixture(scope=" ...
随机推荐
- watch、tail联合使用
因为用了tmux,不想调整窗格大小,只想输出命令结果的最后几行,所以就想出了这个方法. watch.tail联合用法 watch 'echo "`nvidia-smi`" | ta ...
- leyou_07_对数据的操作
1.目标在数据库的两张表中拿到以下数据,并完成状态.搜索和分页功能 实体类Spu(页面需要的数据) 实体类Category(页面需要的数据) name:商品分类 2.分析: 返回的数据在两个实体类中, ...
- Linux下IP修改后重启服务器 oralce 出错(监听无法启动)
针对linux下修改IP导致的Oracle不能启动问题的解决 主要修改/etc/hosts配置文件.修改前配置: # Do not remove the following line, or vari ...
- Office2010 破解(Microsoft Toolkit 2.4.3.exe)
这两天破解刚安装好的office2010,总是报错 刚才重新下载了Microsoft Toolkit 2.4.3.exe工具后,破解成功,操作如下: 选择office按钮后,如下操作,
- megacli在线raid构建详解(转载自用)
版权声明:本文为博主原创文章,未经博主允许不得转载,转载附上原文链接即可. https://blog.csdn.net/GX_1_11_real/article/details/83213959 ht ...
- vue 路由入门(vue-router)
新建的 js 文件如下: import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter) //全局使用该组件 / ...
- PAT甲级——A1059 Prime Factors
Given any positive integer N, you are supposed to find all of its prime factors, and write them in t ...
- PAT甲级——A1031 Hello World for U
Given any string of N (≥) characters, you are asked to form the characters into the shape of U. For ...
- 《DSP using MATLAB》Problem 7.28
又是一年五一节,朋友圈都是晒名山大川的,晒脑袋的,我这没钱的待在家里上网转转吧 频率采样法设计带通滤波器,过渡带中有一个样点 代码: %% ++++++++++++++++++++++++++++++ ...
- java求1000以内的水仙花数
水仙花数是指一个 n 位数 ( n>=3 ),它的每个位上的数字的 n 次幂之和等于它本身.(例如:1^3 + 5^3 + 3^3 = 153) 三位的水仙花数共有4个,分别为:153.370. ...