翻译 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 浏览: 12
这段代码的作用是注册命令行参数和 ini 配置文件的选项,一次性在测试运行开始时调用。注意,这个函数应该只在插件或者 conftest.py 文件中实现,因为 pytest 在启动时会发现插件的位置。函数有两个参数,parser 和 pluginmanager。parser 用来添加命令行选项和 ini 文件值,pluginmanager 可以用来安装 hookspec() 或 hookimpl(),允许一个插件调用另一个插件的 hook 来改变如何添加命令行选项。选项可以在后面通过 config 对象访问,config.getoption(name) 用来获取命令行选项的值,config.getini(name) 用来获取从 ini 文件读取的值。config 对象通过 .config 属性在许多内部对象中传递,也可以通过 pytestconfig fixture 来获取。
相关问题
pytest_addoption
`pytest_addoption` is a hook function in pytest that allows you to define command-line options that can be used when running your tests. This function is called once at the beginning of the test session and can be used to add options to the `pytest` command.
You can use `pytest_addoption` to add custom options to `pytest`, such as specifying a test database, specifying a directory to search for test files, or specifying a test configuration file. These options can then be accessed in your test functions through the `request` fixture.
Here's an example of how to use `pytest_addoption` to add a custom `--env` option to `pytest`:
```python
# conftest.py
def pytest_addoption(parser):
parser.addoption("--env", action="store", default="dev",
help="Specify the test environment: dev, stage or prod")
# test_example.py
def test_example(request):
env = request.config.getoption("--env")
assert env == "dev"
```
In this example, we define a custom `--env` option in `pytest_addoption` and then use the `request` fixture to retrieve the value of this option in our test function. If no `--env` option is specified when running the tests, the default value of `"dev"` will be used.
翻译 pytest_collection(session)[source] Perform the collection phase for the given session. Stops at first non-None result, see firstresult: stop at first non-None result. The return value is not used, but only stops further processing. The default collection phase is this (see individual hooks for full details): Starting from session as the initial collector: pytest_collectstart(collector) report = pytest_make_collect_report(collector) pytest_exception_interact(collector, call, report) if an interactive exception occurred For each collected node: If an item, pytest_itemcollected(item) If a collector, recurse into it. pytest_collectreport(report) pytest_collection_modifyitems(session, config, items) pytest_deselected(items) for any deselected items (may be called multiple times) pytest_collection_finish(session) Set session.items to the list of collected items Set session.testscollected to the number of collected items You can implement this hook to only perform some action before collection, for example the terminal plugin uses it to start displaying the collection counter (and returns None). Parameters session (Session) – The pytest session object.
函数 pytest_collection(session) 的作用是执行给定会话的收集阶段。如果有第一个非 None 的结果,函数会停止执行,这受 firstresult 参数的控制。返回值不会被使用,但会停止进一步的处理过程。默认的收集阶段如下(请查看各个钩子的完整细节):
从 session 作为初始收集器开始:
pytest_collectstart(collector)
report = pytest_make_collect_report(collector)
pytest_exception_interact(collector, call, report) 如果发生交互式异常
对于每个收集到的节点:
如果是 item,则执行 pytest_itemcollected(item)
如果是 collector,则递归进入它
pytest_collectreport(report)
pytest_collection_modifyitems(session, config, items)
对于任何已取消选择的项目,执行 pytest_deselected(items)(可能会被多次调用)
pytest_collection_finish(session)
将 session.items 设置为收集到的项目列表
将 session.testscollected 设置为收集到的项目数量
你可以实现这个钩子来只在收集前执行某些操作,例如终端插件使用它来开始显示收集计数器(并返回 None)。
参数 session(Session):pytest 会话对象。