我有一些代码如下所示.我运行时遇到too few args错误.我没有setup_class明确地调用,所以不确定如何传递任何参数.我试过装饰方法@classmethod,但仍然看到相同的错误.
我看到的错误是 - E TypeError: setup_class() takes exactly 2 arguments (1 given)
需要注意的一点 - 如果我没有将任何参数传递给类,只传递cls,那么我没有看到错误.
任何帮助是极大的赞赏.
在发布之前,我确实在问题#1和问题#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
Run Code Online (Sandbox Code Playgroud) 我正在使用 pytest,通常将我的测试分组为包中模块的“镜像”。为了在我的测试模块中有一个良好的结构,我喜欢将一些测试分组到类中,即使我使用的是 pytest。我\xc2\xb4ve遇到了灯具范围级别的问题。考虑这个最小的例子:
\n\nimport pytest\n\n\n@pytest.fixture(scope=\'module\')\ndef fixture_a():\n return 2\n\n\nclass TestExample:\n b = 1.\n\n @pytest.fixture(autouse=True, scope=\'function\')\n def add_info(self, fixture_a):\n self.c = self.b * fixture_a\n\n def test_foo(self):\n assert self.c + self.b == 3\n\n def test_bar(self):\n assert self.c * self.b == 2\nRun Code Online (Sandbox Code Playgroud)\n\n这是有效的,但是“setup”执行两次,即每个测试方法执行一次。我希望每个类实例只执行一次,但是当将固定装置范围更改为“类”时,我得到:
\n\nFAILED [ 50%]\ntests\\tests_simple\\test_library\\test_example_sof.py:15 (TestExample.test_foo)\nself = <test_example_sof.TestExample object at 0x0000019A8C9C9CC0>\n\n def test_foo(self):\n> assert self.c + self.b == 3\nE AttributeError: \'TestExample\' object has no attribute \'c\'\n\ntest_example_sof.py:17: AttributeError\nFAILED [100%]\ntests\\tests_simple\\test_library\\test_example_sof.py:18 (TestExample.test_bar)\nself = <test_example_sof.TestExample object at 0x0000019A8C9C9EF0>\n\n def test_bar(self):\n> assert self.c * self.b …Run Code Online (Sandbox Code Playgroud)