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

上图是试验的目录结构

conftest.py:存放pytest fixture的文件

 1 import uuid
 2 import pytest
 3
 4 @pytest.fixture(scope="module")
 5 def test_module():
 6     return "module"+str(uuid.uuid4())
 7
 8 @pytest.fixture(scope="class")
 9 def test_class():
10     return "class"+str(uuid.uuid4())
11
12 @pytest.fixture(scope="function")
13 def test_function():
14     return "function"+str(uuid.uuid4())
15
16 @pytest.fixture(scope="session")
17 def test_session():
18     return "session"+str(uuid.uuid4())
19
20
21 @pytest.fixture(scope="package")
22 def test_package():
23     return "package"+str(uuid.uuid4())

test_class.py:类测试文件

 1 class Test_for_pytest_scope(object):
 2     def test_case1(self,test_module,test_class,test_function,test_session,test_package):
 3         print("testcase/test_class.py-test_case1||" + test_function)
 4         print("testcase/test_class.py-test_case1||"+test_module)
 5         print("testcase/test_class.py-test_case1||"+test_class)
 6         print("testcase/test_class.py-test_case1||" + test_session)
 7         print("testcase/test_class.py-test_case1||" + test_package)
 8
 9     def test_case2(self,test_module,test_class,test_function,test_session,test_package):
10         print("testcase/test_class.py-test_case2||" + test_function)
11         print("testcase/test_class.py-test_case2||"+test_module)
12         print("testcase/test_class.py-test_case2||"+test_class)
13         print("testcase/test_class.py-test_case2||" + test_session)
14         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还处于试验阶段。

原文地址:https://www.cnblogs.com/moonpool/p/11350780.html

时间: 2024-08-26 21:29:53

pytest fixture中scope试验,包含function、module、class、session、package的相关文章

pytest 用 @pytest.mark.usefixtures(&quot;fixtureName&quot;)或@pytest.fixture(scope=&quot;function&quot;, autouse=True)装饰,实现类似setup和TearDown的功能

conftest.py import pytest @pytest.fixture(scope="class") def class_auto(): print("") print("class-begin") yield print("class-end") test_autouse.py 1 import pytest 2 3 4 @pytest.mark.usefixtures("class_auto"

abap function module中的异常处理

1: 定义一个有异常抛出的function module.  (zfm_moudle6),  该函数中有符合exceptions中的异常,将会自动将exceptions中的异常抛出. FUNCTION ZFM_MOUDLE6. *"---------------------------------------------------------------------- *"*"Local Interface: *" RAISING *" CX_SY_AR

pytest.fixture的初始化清除操作

需要导入模块pytest 使用装饰器:pytest.fixture(scope='function',autouse=False) fixture()函数参数解释说明 fixture里面有个scope参数可以控制fixture的作用范围:session>module>class>function -function:每一个函数或方法都会调用 -class:每一个类调用一次,一个类中可以有多个方法 -module:每一个.py文件调用一次,该文件内又有多个function和class -s

Lua中的模块(module)和包(package)详解1

这篇文章主要介绍了Lua中的模块(module)和包(package)详解,本文讲解了require函数.写一个模块.package.loaded.module函数等内容,需要的朋友可以参考下 前言 从Lua5.1版本开始,就对模块和包添加了新的支持,可是使用require和module来定义和使用模块和包.require用于使用模块,module用于创建模块.简单的说,一个模块就是一个程序库,可以通过require来加载.然后便得到了一个全局变量,表示一个table.这个table就像是一个命

Lua中的模块(module)和包(package)详解

这篇文章主要介绍了Lua中的模块(module)和包(package)详解,本文讲解了require函数.写一个模块.package.loaded.module函数等内容,需要的朋友可以参考下 前言 从Lua5.1版本开始,就对模块和包添加了新的支持,可是使用require和module来定义和使用模块和包.require用于使用模块,module用于创建模块.简单的说,一个模块就是一个程序库,可以通过require来加载.然后便得到了一个全局变量,表示一个table.这个table就像是一个命

AngularJS中Scope间通讯Demo

在AngularJS中,每一个controller都有对应的Scope,而Scope间有时候需要通讯.比如有如下的一个controller嵌套: <body ng-controller="AppCtrl"> <table ng-controller = "ProductCtrl"> ... <tr ng-repeat="product in products"> <td>{{$index + 1}}

无法解析的外部符号 _ ,该符号在函数 &quot; __main中被引用 &amp;&amp; This function or variable may be unsafe.

加上winsock2的运行库看看吧,一般不用加的啊 #pragma comment(lib,"Ws2_32.lib") 应该就没有问题了吧^_^ 以下为转载 http://blog.chinaunix.net/uid-20672257-id-2955771.html 1.将过去的工程用VS2010打开的时候.你有可能会遇到一大堆的警告:warning C4996. 比如:warning C4996: 'sprintf': This function or variable may be

在 eclipse中 创建 web 版的 maven 工程以及 进一步了解 pom.xml 中 scope 的相关配置设置问题

1.在 eclipse中 创建 web 版的 maven 工程: a.和 创建 java 版的 maven 工程一样,先选中一个简单的工程(跳过经典模式),后填写坐标(不了解的看先前的博文),在打包方式中选择 war 包,后点击 finish. b.可以看到,在新建 的 javaproject03 的 项目中,只有 webapp目录,而实际的内容并没有.这里我们在每次新建时都要做设置.设置如下: 注:鼠标停在所建的项目上,点击右键进入 properties --> Project Facets

maven 中scope 的应用

maven 中scope 定义了这个包的应用范围,根据场景不同某些jar的应用也不同,scope 有5种类型: 以下内容是转帖复制: 一.compile:编译范围compile是默认的范围:如果没有提供一个范围,编译范围依赖在所有的classpath 中可用,同时它们也会被打包.而且这些dependency会传递到依赖的项目中. 二.provided:已提供范围provided 明了dependency 由JDK或者容器提供.例如如果开发了一个web 应用,可能在编译 classpath 中需要