python - 如何让 pytest 忽略名为 TestSomething 的类?
问题描述
我正在研究一种测试框架,TestSomething
当然它恰好有类命名。而且我意识到我的测试失败了,因为 pytest 将这些类视为“我需要实例化和运行的东西!”,一旦导入。而这绝对行不通。
import pytest
from package import TestSomethingClass
是否可以直接从 pytest 测试文件中导入此类?或者我应该通过固定装置间接使用它们吗?
同样的问题也适用于异常,因为我需要做类似的事情
with pytest.raises(TestsSomethingError):
解决方案
显式禁用
你可以允许 pytest 忽略这个特定的类,因为它以 word 开头Test
,通过将 __test__
标志设置False
在你的冲突类中
class TestSomethingClass(object):
__test__ = False
def test_class_something(self, object):
pass
功能:https ://github.com/pytest-dev/pytest/pull/1561
配置文件
我们还可以在您的文件中完全更改约定pytest.ini
并忽略所有以单词开头的类名Example
。但我不建议这样做,因为我们可能会在未来遇到意想不到的后果。
#pytest.ini file
[pytest]
python_classes = !Example
Pytest 约定
在类外测试前缀测试函数或方法
测试前缀测试类中的测试前缀测试函数或方法(没有init方法)
推荐阅读
- json - 如何保证matlab的jsonencode总是输出一个向量
- java - 重新下载时保存您的应用数据
- python - 是否有任何支持“突出显示并运行”的python IDE?
- php - array_keys vs array_flip 基准测试请求
- c# - 在与 C# 不同的地方显示组合框选择
- python - PySpark 2 - 正则表达式替换之前的所有内容
- angular - Angular 2:“期望操作符具有相似类型或任何”linting 错误
- php - Sleep() 不会延迟表单操作
- php - Lumen 文档中的“function () use ($app) {”行是什么意思(可能在 Laravel 中有效)
- javascript - 使用 vuejs 复制 innerHTML 输出值