4.pytest 启动入口
pytest 的启动方式有三种:
- 命令行
pytest
2. 命令行
python -m pytest
3. python 代码
import pytest
pytest.main()
这三种方式都将执行_pytest.config.main 函数, 其源码如下
def main(
args: Optional[Union[List[str], "os.PathLike[str]"]] = None,
plugins: Optional[Sequence[Union[str, _PluggyPlugin]]] = None,
) -> Union[int, ExitCode]:
"""Perform an in-process test run.
:param args: List of command line arguments.
:param plugins: List of plugin objects to be auto-registered during initialization.
:returns: An exit code.
"""
try:
try:
config = _prepareconfig(args, plugins)
except ConftestImportFailure as e:
exc_info = ExceptionInfo.from_exc_info(e.excinfo)
tw = TerminalWriter(sys.stderr)
tw.line(f"Import

本文分析了pytest的启动入口,主要通过三种方式启动:pytest命令行、python-m pytest和import pytest.main。核心是调用_pytest.config.main函数,该函数负责加载配置、调用插件hook并返回退出代码。了解到pytest内部大量使用插件,构建出丰富的生态系统。后续将深入研究插件系统pluggy。
最低0.47元/天 解锁文章
997

被折叠的 条评论
为什么被折叠?



