本文系《pytest源码剖析》系列内容
13. legacypath
插件路径:_pytest.legacypath
实现的 hook
调用的 hook
无
定义的 fixture
tmpdir_factory
tmpdir
插件功能
创建插件legacypath-tmpdir
创建插件legacypath-pytester
供内部插件pytester使用
makedir
fspath
startdir
invocation_dir
rootdir
inifile
startdir
_getini_unknown_type
fspath
代码片段
@hookimpldef pytest_configure(config: Config) -> None: if config.pluginmanager.has_plugin("tmpdir"): mp = MonkeyPatch() config.add_cleanup(mp.undo) try: tmp_path_factory = config._tmp_path_factory # type: ignore[attr-defined] except AttributeError:# tmpdir plugin is blocked. pass else: _tmpdirhandler = TempdirFactory(tmp_path_factory, _ispytest=True) mp.setattr(config, "_tmpdirhandler", _tmpdirhandler, raising=False)
config.pluginmanager.register(LegacyTmpdirPlugin, "legacypath-tmpdir")
本插件依赖另外的插件:tmpdir和monkeypatch
简评
这是一个过渡性插件:
起因是py已经停止开发,其中的py.test独立为 pytest 蓬勃发展,
所以需要从pytest中彻底移除对py的依赖,才能继续前进
这其中涉及到将代码中使用的py.path替换成 python 标准库pathlib。
考虑到这么大的变更会影响上下游,于是 pytest 开发团队提出一个非常精彩的方案:
将老旧的核心代码改为可插拔的插件
插件默认启用,使得老用户可以用旧方式继续用运行
通过禁用插件,即可屏蔽旧代码,验证和预览彻底移除效果
在较长的一段时间里,继续保持兼容和验证
下一个大版本中,移除插件,完成平滑过度
相关讨论:
https://github.com/pytest-dev/pytest/pull/9208
https://github.com/pytest-dev/pytest/issues/9283
...
本插件提供了两个 fixture:tmpdir_factory和tmpdir,用于在测试时创建临时目录和文件
在新的项目中,建议改用tmpdir插件提供的新 fixture:tmp_path_factory和tmp_path
领取专属 10元无门槛券
私享最新 技术干货