Python pytest不允许使用非静态方法的类
我遵循了pytest的本教程: 但注意到如果我创建:Python pytest不允许使用非静态方法的类,python,unit-testing,oop,static-methods,pytest,Python,Unit Testing,Oop,Static Methods,Pytest,我遵循了pytest的本教程: 但注意到如果我创建: class ComplexTestClass: """ Complex and more nuanced tests for specific sentences """ def test_multiple_sentences(): 如果我没有在函数的顶部添加@staticmethod,我会在函数上出现一个错误,它无法通过()is识别,并且在我的PyCharm中以红色突出显示 教程(显示不需要静态方法)是错
class ComplexTestClass:
"""
Complex and more nuanced tests for specific sentences
"""
def test_multiple_sentences():
如果我没有在函数的顶部添加@staticmethod
,我会在函数上出现一个错误,它无法通过()
is识别,并且在我的PyCharm中以红色突出显示
教程(显示不需要静态方法)是错误的,还是我错了并且将使我的测试无效?如果您没有声明方法为
@staticmethod
,则需要方法中的self
参数:
def test_multiple_sentences(self):
根据
pytest
文档,为什么它有没有self
和\u init
的示例?我的目标是在运行测试时在pytest
及其所有方法中调用该类。当我运行pytest-q test_text.py
时,staticmethod是否会阻止这种情况发生?您链接到的确切示例确实有self
作为参数。该页上没有self
参数的示例是在类
之外定义的函数,而不是方法。遵循链接到的示例(即不要将测试方法声明为@staticmethod
s并包含self
参数),所有测试用例都应该运行。