我有一些代码,如下所示。运行它时出现too few args
错误。我没有明确调用setup_class
,因此不确定如何将任何参数传递给它。我尝试用@classmethod
装饰该方法,但仍然看到相同的错误。
我看到的错误是这个-E TypeError: setup_class() takes exactly 2 arguments (1 given)
需要注意的一点-如果我没有将任何参数传递给类,而仅传递了cls
,则我看不到错误。
非常感谢您的帮助。
我在发布之前确实审核了这些问题question #1和question #2。我不了解针对这些问题发布的解决方案或它们的工作方式。
class A_Helper:
def __init__(self, fixture):
print "In class A_Helper"
def some_method_in_a_helper(self):
print "foo"
class Test_class:
def setup_class(cls, fixture):
print "!!! In setup class !!!"
cls.a_helper = A_Helper(fixture)
def test_some_method(self):
self.a_helper.some_method_in_a_helper()
assert 0 == 0
您收到此错误是因为您试图混合使用py.test支持的两种独立测试样式:经典单元测试和pytest的固定装置。
我建议不要混合使用它们,而只是像这样定义一个类范围的灯具:
import pytest
class A_Helper:
def __init__(self, fixture):
print "In class A_Helper"
def some_method_in_a_helper(self):
print "foo"
@pytest.fixture(scope='class')
def a_helper(fixture):
return A_Helper(fixture)
class Test_class:
def test_some_method(self, a_helper):
a_helper.some_method_in_a_helper()
assert 0 == 0
由于您在pytest中使用了它,它将仅使用一个参数和一个参数来调用setup_class
,看起来您无需更改pytest calls this的方式就无法更改它。
[您应该只遵循documentation并按指定的方式定义setup_class
函数,然后在该方法中使用您需要在该函数中使用的自定义参数来设置类,看起来像]
class Test_class:
@classmethod
def setup_class(cls):
print "!!! In setup class !!!"
arg = '' # your parameter here
cls.a_helper = A_Helper(arg)
def test_some_method(self):
self.a_helper.some_method_in_a_helper()
assert 0 == 0