Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pytest不允许使用非静态方法的类_Python_Unit Testing_Oop_Static Methods_Pytest - Fatal编程技术网

Python 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中以红色突出显示 教程(显示不需要静态方法)是错

我遵循了pytest的本教程:

但注意到如果我创建:

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
参数),所有测试用例都应该运行。