弃用和移除¶
此页列出了当前不推荐使用或在过去的主要版本中已删除的所有pytest功能。目的是给用户一个明确的理由,为什么某个特性被删除,以及应该使用什么替代方法。
不推荐使用的功能¶
下面是所有被视为已弃用的pytest特性的完整列表。使用这些功能将导致 PytestWarning 或子类,可以使用 standard warning filters .
饲养 unittest.SkipTest 在收集过程中¶
6.3 版后已移除.
饲养 unittest.SkipTest 不建议在pytest收集阶段跳过测试收集。使用 pytest.skip() 相反。
注意:此弃用仅与使用 unittest.SkipTest 在测试收集期间。你可能没有那么做。的常见用法 unittest.SkipTest / unittest.TestCase.skipTest() / unittest.skip() 在单元测试中,完全支持测试用例。
这个 --strict 命令行选项¶
6.2 版后已移除.
这个 --strict 命令行选项已弃用,取而代之的是 --strict-markers ,这更好地传达了选项的作用。
我们有计划也许在未来重新引入 --strict 并使其成为所有与严格相关的选项的包含标志 (--strict-markers 和 --strict-config 目前,未来可能会推出更多)。
这个 yield_fixture 功能/装饰品¶
6.2 版后已移除.
pytest.yield_fixture 是的不推荐使用的别名 pytest.fixture() .
它已经这样很长时间了,所以可以安全地搜索/更换。
这个 pytest_warning_captured 钩¶
6.0 版后已移除.
这个钩子有一个 item 无法由序列化的参数 pytest-xdist .
使用 pytest_warning_recored 钩子代替了 item 参数 nodeid 参数。
这个 pytest._fillfuncargs 功能¶
6.0 版后已移除.
保留此函数是为了与旧插件向后兼容。
它的功能不是要直接使用的,但是如果必须替换它,请使用 function._request._fillfixtures() 相反,请注意,这不是一个公共API,将来可能会中断。
删除的功能¶
如我方所述 向后兼容策略 策略,不推荐的功能只有在经过适当的不推荐期后才在主要版本中删除。
--no-print-logs 命令行选项¶
5.4 版后已移除.
Removed in version 6.0.
这个 --no-print-logs 期权和 log_print ini设置被删除。如果你用过,请用 --show-capture 相反。
A --show-capture 命令行选项已添加到中 pytest 3.5.0 允许指定在测试失败时如何显示捕获的输出: no , stdout , stderr , log 或 all (默认值)。
结果日志 (--result-log )¶
4.0 版后已移除.
Removed in version 6.0.
这个 --result-log 选项生成一个可以在运行时分析的测试报告流,但它使用一种自定义格式,要求用户实现自己的解析器。
这个 pytest-reportlog 插件提供了一个 --report-log 生成一个可扩展的JSON对象,并为每个对象生成更多的可扩展实例。请尝试一下并提供反馈。
这个 pytest-reportlog 插件甚至可能在某个时候合并到核心中,这取决于插件的计划和使用它的用户数量。
pytest_collect_directory 钩¶
Removed in version 6.0.
这个 pytest_collect_directory 多年来一直没有正常工作(它被调用,但结果被忽略)。用户可以考虑使用 pytest_collection_modifyitems 相反。
TerminalReporter.writer¶
Removed in version 6.0.
这个 TerminalReporter.writer 属性已被弃用,不应再使用。这是不经意间作为插件的公共API的一部分而暴露出来的,它与 py.io.TerminalWriter .
使用的插件 TerminalReporter.writer 直接应改为使用 TerminalReporter 提供相同功能的方法。
junit_family 默认值更改为“xunit2”¶
在 6.0 版更改.
默认值为 junit_family 选项将更改为 xunit2 在pytest6.0中,它是旧版本的更新 xunit1 格式,并且在操作此类文件的现代工具(例如,Jenkins、Azure Pipelines等)中默认支持。
建议用户尝试新的 xunit2 格式化并查看他们使用junitxml文件的工具是否支持它。
要使用新格式,请更新 pytest.ini :
[pytest]
junit_family=xunit2
如果您发现工具不支持新格式,并希望继续使用旧版本,请将选项设置为 legacy 而是:
[pytest]
junit_family=legacy
通过使用 legacy 升级到pytest 6.0时,您将继续使用传统的/xunit1格式,默认格式为 xunit2 .
为了让用户了解转换,pytest将发出警告,以防 --junitxml 选项在命令行中给出,但是 junit_family 未在中显式配置 pytest.ini .
已知支持 xunit2 格式:
节点构造更改为 Node.from_parent¶
在 6.0 版更改.
节点的构造现在应该使用命名构造函数 from_parent . api表面中的这一限制旨在实现集合树的更好/更简单的重构。
这意味着 MyItem(name="foo", parent=collector, obj=42) 现在必须调用 MyItem.from_parent(collector, name="foo") .
希望支持旧版本pytest并禁止显示警告的插件可以使用 hasattr 检查是否 from_parent 存在于该版本中:
def pytest_pycollect_makeitem(collector, name, obj):
if hasattr(MyItem, "from_parent"):
item = MyItem.from_parent(collector, name="foo")
item.obj = 42
return item
else:
return MyItem(name="foo", parent=collector, obj=42)
注意 from_parent 只应使用参数的关键字参数调用。
funcargnames alias for fixturenames¶
Removed in version 6.0.
这个 FixtureRequest , Metafunc 和 Function 类跟踪其关联设备的名称,并使用适当的名称 fixturenames 属性。
在pytest 2.3之前,此属性已命名 funcargnames 从那以后我们就一直把它作为别名。最后要删除,因为在我们或插件作者必须区分fixture名称和非fixture提供的名称(如 pytest.mark.parametrize .
pytest.config 全球的¶
Removed in version 5.0.
这个 pytest.config 全局对象已弃用。代替使用 request.config (通过 request fixture)或者如果您是插件作者,请使用 pytest_configure(config) 钩子。请注意,许多挂钩也可以访问 config 间接反对,通过 session.config 或 item.config 例如。
"message" parameter of pytest.raises¶
Removed in version 5.0.
认为这个参数将匹配异常消息是一个常见的错误,而实际上它只在 pytest.raises 检查失败。为了防止用户犯下这个错误,并且因为人们认为它很少被使用,Pytest正在贬低它,目前还没有提供替代方案。
如果您对此参数有一个有效的用例,请考虑这样做,以获得可以调用的相同结果。 pytest.fail 在结束时手动 with 语句。
例如:
with pytest.raises(TimeoutError, message="Client got unexpected message"):
wait_for(websocket.recv(), 0.5)
变成:
with pytest.raises(TimeoutError):
wait_for(websocket.recv(), 0.5)
pytest.fail("Client got unexpected message")
如果您仍然对这一折旧和未来的删除有疑虑,请评论 issue #3974 .
raises / warns 以字符串作为第二个参数¶
Removed in version 5.0.
而是使用这些的上下文管理器形式。必要时调用 exec 直接。
例子:
pytest.raises(ZeroDivisionError, "1 / 0")
pytest.raises(SyntaxError, "a $ b")
pytest.warns(DeprecationWarning, "my_function()")
pytest.warns(SyntaxWarning, "assert(1, 2)")
变成:
with pytest.raises(ZeroDivisionError):
1 / 0
with pytest.raises(SyntaxError):
exec("a $ b") # exec is required for invalid syntax
with pytest.warns(DeprecationWarning):
my_function()
with pytest.warns(SyntaxWarning):
exec("assert(1, 2)") # exec is used to avoid a top-level warning
使用 Class 在自定义收集器中¶
Removed in version 4.0.
使用名为的对象 "Class" 作为自定义在 Collector 子类已被弃用。用户应该使用 pytest_pycollect_makeitem 在收集期间自定义节点类型。
此问题只会影响创建新集合类型的高级插件,因此如果看到此警告消息,请与作者联系,以便他们更改代码。
标记在 pytest.mark.parametrize¶
Removed in version 4.0.
对a的值应用标记 pytest.mark.parametrize 现在已弃用调用。例如:
@pytest.mark.parametrize(
"a, b",
[
(3, 9),
pytest.mark.xfail(reason="flaky")(6, 36),
(10, 100),
(20, 200),
(40, 400),
(50, 500),
],
)
def test_foo(a, b):
...
此代码适用于 pytest.mark.xfail(reason="flaky") 标记到 (6, 36) 上述参数化调用的值。
这被认为是很难阅读和理解的,而且它的实现也给代码带来了问题,阻止了Marks体系结构的进一步内部改进。
要更新代码,请使用 pytest.param :
@pytest.mark.parametrize(
"a, b",
[
(3, 9),
pytest.param(6, 36, marks=pytest.mark.xfail(reason="flaky")),
(10, 100),
(20, 200),
(40, 400),
(50, 500),
],
)
def test_foo(a, b):
...
pytest_funcarg__ 前缀¶
Removed in version 4.0.
在早期的pytest版本中,可以使用 pytest_funcarg__ 前缀:
def pytest_funcarg__data():
return SomeData()
切换到 @pytest.fixture 装饰符:
@pytest.fixture
def data():
return SomeData()
[脓包] SETUP.CFG文件中的节¶
Removed in version 4.0.
[pytest] 章节在 setup.cfg 现在应命名文件 [tool:pytest] 以避免与其他distutils命令冲突。
Metafunc.addcall¶
Removed in version 4.0.
Metafunc.addcall 是当前参数化机制的先驱。用户应该使用 pytest.Metafunc.parametrize() 相反。
例子:
def pytest_generate_tests(metafunc):
metafunc.addcall({"i": 1}, id="1")
metafunc.addcall({"i": 2}, id="2")
变成:
def pytest_generate_tests(metafunc):
metafunc.parametrize("i", [1, 2], ids=["1", "2"])
cached_setup¶
Removed in version 4.0.
request.cached_setup 安装/拆卸机构的前身是否可用于固定装置?
例子:
@pytest.fixture
def db_session():
return request.cached_setup(
setup=Session.create, teardown=lambda session: session.close(), scope="module"
)
这应该更新以使用标准夹具机构:
@pytest.fixture(scope="module")
def db_session():
session = Session.create()
yield session
session.close()
你可以咨询 funcarg comparison section in the docs 更多信息。
非顶级conftest文件中的pytest插件¶
Removed in version 4.0.
定义 pytest_plugins 现在在非顶级conftest.py文件中已弃用,因为它们将激活引用的插件 全球地 ,这是令人惊讶的,因为所有其他pytest特性 conftest.py 文件是唯一的 积极的 在它上面或下面的测试。
Config.warn and Node.warn¶
Removed in version 4.0.
这些方法是内部Pytest警告系统的一部分,但自从 3.8 Pytest使用内置警告系统进行警告,因此现在不推荐使用这两个函数。
Config.warn 应替换为调用标准 warnings.warn 例如:
config.warn("C1", "some warning")
变成:
warnings.warn(pytest.PytestWarning("some warning"))
Node.warn 现在支持两个签名:
node.warn(PytestWarning("some message"))现在是 推荐 调用此函数的方法。警告实例必须是pytestwarning或子类。node.warn("CI", "some message"):此代码/消息表单 远离的 并应转换为上面的警告实例窗体。
record_xml_property¶
Removed in version 4.0.
这个 record_xml_property 现在不赞成使用fixture,而赞成使用更通用的fixture record_property ,可供其他consumers使用(例如 pytest-html )获取有关测试运行的自定义信息。
这只是重命名夹具的问题,因为API是相同的:
def test_foo(record_xml_property):
...
改为:
def test_foo(record_property):
...
将命令行字符串传递给 pytest.main()¶
Removed in version 4.0.
将命令行字符串传递给 pytest.main() 被贬低:
pytest.main("-v -s")
改为传递列表:
pytest.main(["-v", "-s"])
通过传递字符串,用户期望pytest将使用他们正在处理的shell规则(例如 bash 或 Powershell 但这很难/不可能用便携式方式完成。
直接调用设备¶
Removed in version 4.0.
不推荐直接调用fixture函数,而不是在测试函数中请求它们。
例如:
@pytest.fixture
def cell():
return ...
@pytest.fixture
def full_cell():
cell = cell()
cell.make_full()
return cell
对于新用户来说,这是一个很大的困惑源,他们通常会调用fixture函数,并从测试函数中交替请求它们,这打破了fixture解析模型。
在这些情况下,只需直接请求从属设备中的功能:
@pytest.fixture
def cell():
return ...
@pytest.fixture
def full_cell(cell):
cell.make_full()
return cell
或者,如果fixture函数在测试中被多次调用(使其难以应用上述模式),或者如果您希望对代码进行最小的更改,则可以创建一个fixture,它与 name 参数:
def cell():
return ...
@pytest.fixture(name="cell")
def cell_fixture():
return cell()
yield 测验¶
Removed in version 4.0.
支持Pytest yield -样式测试,其中测试函数 yield 然后转化为适当测试方法的函数和值。例子:
def check(x, y):
assert x ** x == y
def test_squared():
yield check, 2, 4
yield check, 3, 9
这将导致生成两个实际的测试函数。
这种形式的测试功能不支持正确的夹具,用户应该切换到 pytest.mark.parametrize :
@pytest.mark.parametrize("x, y", [(2, 4), (3, 9)])
def test_squared(x, y):
assert x ** x == y
通过访问内部类 Node¶
Removed in version 4.0.
访问 Module , Function , Class , Instance , File 和 Item 通过 Node 实例现在发出此警告:
usage of Function.Module is deprecated, please use pytest.Module instead
用户应该 import pytest 并使用 pytest 模块。
这已经被记录为弃用多年,但直到现在,我们才真正发出弃用警告。
Node.get_marker¶
Removed in version 4.0.
作为大的一部分 标记更新和迭代 , _pytest.nodes.Node.get_marker 已删除。看到了吗 the documentation 关于如何更新代码的提示。
somefunction.markname¶
Removed in version 4.0.
作为大的一部分 标记更新和迭代 我们已经不赞成使用 MarkInfo 获取元素标记的唯一正确方法是通过 node.iter_markers(name) .
pytest_namespace¶
Removed in version 4.0.
这个钩子不推荐使用,因为它使Pytest内部在配置和初始化方面变得非常复杂,使得一些错误修复和重构变得不可能。
使用示例:
class MySymbol:
...
def pytest_namespace():
return {"my_symbol": MySymbol()}
依赖这个钩子的插件作者应该要求用户现在直接导入插件模块(使用适当的公共API)。
作为一种权宜之计,插件作者仍然可以将自己的名字注入pytest的名称空间,通常在 pytest_configure :
import pytest
def pytest_configure():
pytest.my_symbol = MySymbol()