翻译 pytest_addoption(parser, pluginmanager)[source] Register argparse-style options and ini-style config values, called once at the beginning of a test run. Note This function should be implemented only in plugins or conftest.py files situated at the tests root directory due to how pytest discovers plugins during startup. Parameters parser (pytest.Parser) – To add command line options, call parser.addoption(...). To add ini-file values call parser.addini(...). pluginmanager (pytest.PytestPluginManager) – The pytest plugin manager, which can be used to install hookspec()’s or hookimpl()’s and allow one plugin to call another plugin’s hooks to change how command line options are added. Options can later be accessed through the config object, respectively: config.getoption(name) to retrieve the value of a command line option. config.getini(name) to retrieve a value read from an ini-style file. The config object is passed around on many internal objects via the .config attribute or can be retrieved as the pytestconfig fixture.
时间: 2024-04-06 18:35:21 浏览: 103
pytest-json-report::spiral_notepad:一个pytest插件,将测试结果报告为JSON
这段代码的作用是注册命令行参数和 ini 配置文件的选项,一次性在测试运行开始时调用。注意,这个函数应该只在插件或者 conftest.py 文件中实现,因为 pytest 在启动时会发现插件的位置。函数有两个参数,parser 和 pluginmanager。parser 用来添加命令行选项和 ini 文件值,pluginmanager 可以用来安装 hookspec() 或 hookimpl(),允许一个插件调用另一个插件的 hook 来改变如何添加命令行选项。选项可以在后面通过 config 对象访问,config.getoption(name) 用来获取命令行选项的值,config.getini(name) 用来获取从 ini 文件读取的值。config 对象通过 .config 属性在许多内部对象中传递,也可以通过 pytestconfig fixture 来获取。
阅读全文