Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pytest 2.3中的封装范围固定装置_Python_Pytest - Fatal编程技术网

Python pytest 2.3中的封装范围固定装置

Python pytest 2.3中的封装范围固定装置,python,pytest,Python,Pytest,在pytest的最新版本中,很容易创建功能、类、模块或会话范围如下的装置: @pytest.fixture(scope="module") def db(): return DB() 这将创建一个fixture,该fixture对于使用它的每个python模块只调用一次 但是对于每个python包需要调用一次的fixture呢? (使用nose,可以使用软件包的\uuu init\uuuuuuuuuuuuuupy中的设置/拆卸方法来完成)对于软件包或目录级装置,可以使用scope=

在pytest的最新版本中,很容易创建功能、类、模块或会话范围如下的装置:

@pytest.fixture(scope="module") 
def db():
     return DB()
这将创建一个fixture,该fixture对于使用它的每个python模块只调用一次

但是对于每个python包需要调用一次的fixture呢?
(使用nose,可以使用软件包的
\uuu init\uuuuuuuuuuuuuupy
中的设置/拆卸方法来完成)

对于软件包或目录级装置,可以使用
scope='session'
在需要的目录中的
conftest.py
文件中声明装置。一旦包/目录中的第一个测试使用了fixture,它就会被实例化。然而,如果fixture函数注册了一个终结器,那么您可能会看到它在该目录中的最后一次测试之后不直接执行。我认为pytest可以支持更急切的拆卸,或者在需要时引入“目录”范围。通常,如果拆卸执行得稍晚一点,只要执行得不太早,这并不是什么大问题:)还要注意


无论如何,如果您需要更急切/精确的pytest拆卸,请随时进行

我认为基于目录的作用域是个坏主意,因为一个包可能有多个目录。包级别可能更有意义,但即使如此,我也不确定。现在在2018年,我注意到支持的包范围已经添加到fixture中。但是正如您所提到的,如果fixture函数注册了一个终结器,那么它不会在该目录中的最后一次测试之后直接执行。这不是包级拆卸的预期行为。我认为Robotframework以更容易理解的方式实现了安装拆卸。