我正在尝试使用 Pytest 创建一个测试环境。这个想法是将测试方法分组到类中。
对于每个班级/小组,我想附上config
将要参数化的夹具。这样我就可以使用“配置 A”运行所有测试,然后使用“配置 B”运行所有测试,依此类推。
但同时,我也想要一个reset
Fixture,可以在类的特定方法或所有方法之前执行。
我遇到的问题是,一旦我应用我的reset
固定装置(对于一个方法或整个类),config
Fixture 似乎在函数作用域而不是类作用域中工作。所以,一旦我应用了reset
夹具、config
Fixture 在类中的每个方法之前/之后调用。
下面的代码重现了这个问题:
import pytest
from pytest import *
@fixture(scope='class')
def config(request):
print("\nconfiguring with %s" % request.param)
yield
print("\ncleaning up config")
@fixture(scope='function')
def reset():
print("\nreseting")
@mark.parametrize("config", ["config-A", "config-B"], indirect=True)
#@mark.usefixtures("reset")
class TestMoreStuff(object):
def test_a(self, config):
pass
def test_b(self, config):
pass
def test_c(self, config):
pass
测试显示了如何config
固定装置应该可以工作,整个班级只执行一次。如果您取消注释usefixtures
装饰,你可以注意到config
固定装置将在每个测试方法中执行。是否可以使用reset
固定装置而不触发此行为?
正如我在评论中提到的,这似乎是 Pytest 3.2.5 中的一个错误。
有一个解决方法,即“强制”参数化的范围。因此,在这种情况下,如果您包括scope="class"
in the parametrize
装饰器,你会得到想要的行为。
import pytest
from pytest import *
@fixture(scope='class')
def config(request):
print("\nconfiguring with %s" % request.param)
yield
print("\ncleaning up config")
@fixture(scope='function')
def reset():
print("\nreseting")
@mark.parametrize("config", ["config-A", "config-B"], indirect=True, scope="class")
@mark.usefixtures("reset")
class TestMoreStuff(object):
def test_a(self, config):
pass
def test_b(self, config):
pass
def test_c(self, config):
pass
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)