我正在开发一种测试框架,它恰好有名为“TestSomething当然”的类。我意识到我的测试失败了,因为 pytest 将这些类视为“我需要实例化和运行的东西!”,一旦导入。而这绝对行不通。
import pytest
from package import TestSomethingClass
Run Code Online (Sandbox Code Playgroud)
是否可以直接从 pytest 测试文件导入此类?或者我应该通过固定装置间接使用它们?
同样的问题也适用于异常,因为我需要做类似的事情
with pytest.raises(TestsSomethingError):
Run Code Online (Sandbox Code Playgroud)
D S*_*iro 10
您可以允许 pytest 忽略这个特定的类,因为它以单词 开头Test,通过将 __test__标志设置在False冲突的类中
class TestSomethingClass(object):
__test__ = False
def test_class_something(self, object):
pass
Run Code Online (Sandbox Code Playgroud)
功能: https: //github.com/pytest-dev/pytest/pull/1561
我们还可以完全更改文件中的约定pytest.ini,并忽略所有以 . 开头的类名Example。但我不建议这样做,因为我们可能会在未来遇到意想不到的后果。
#pytest.ini file
[pytest]
python_classes = !Example
Run Code Online (Sandbox Code Playgroud)
在类外测试带前缀的测试函数或方法
Test 前缀测试函数或 Test 前缀测试类中的方法(没有init方法)
来源: https: //docs.pytest.org/en/stable/customize.html