使用pytest参数化fixture运行我得到的“self”没有定义

2024-10-01 13:38:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我上这个班:

在接受.py在

class AcceptC(object):

    def __init__(self):
        self.minimum = 30
        self.maximum = 40

以及单元测试:

接受_测试.py在

^{pr2}$

使用pytest运行时,我收到一个错误:

NameError: name 'self' is not defined

我还看到我不能在测试函数中使用self作为参数。在

最后,有没有办法避免使用:

expected_min = self.accept.minimum
expected_max = self.accept.maximum

并立即使用self.accept.minimum?在

如果我使用self作为测试函数的参数,它将返回:

fixture 'self' not found


Tags: pyself参数objectinitdefnot单元测试
2条回答

您的方法中缺少self(假设它是一个方法,如果它是一个函数,则需要先使它成为TestAcceptC的方法)参数列表:

def test_init_returns_correct_results(minimum, maximum, expected_min, expected_max):

应该是(我做了一些额外的改变;远离unittest和更多{}-类):

^{pr2}$

问题仅仅是因为:

class TestAcceptC(unittest.TestCase):

这个遗产unittest.TestCase会破坏pytest固定装置的“自我”。在

信息:https://docs.pytest.org/en/3.0.1/unittest.html

相关问题 更多 >