python - Pytest 插件 : Overriding pytest_runtest_call and friends

标签 python plugins pytest

我正在使用 pytest 为我的项目开发一个测试套件。由于项目的性质,我需要创建一个 Pytest 插件来控制测试的运行方式;它们不是在本地运行,而是发送到不同的进程运行。 (我知道 xdist 但我认为它不能解决我的问题。)

我一直在通过覆盖各种 pytest_runtest_* 方法来编写自己的 Pytest 插件。到目前为止,进展顺利。这是我碰壁的地方:我希望我的 pytest_runtest_setuppytest_runtest_callpytest_runtest_teardown 实现真正负责进行设置,通话和拆机。他们将在不同的过程中进行。 我的问题是:在 Pytest 调用我的 pytest_runtest_setup 之后,它还会调用所有其他 pytest_runtest_setup 插件线。这是因为 pytest_runtest_setup 的钩子(Hook)规范有 firstresult=False

我不想要这个,因为我不希望 pytest_runtest_setup 在当前进程上实际运行。我想负责自己运行它。我想覆盖它的运行方式,而不是添加。我希望低于我自己的 pytest_runtest_setup 的其他实现运行。

我该怎么做?

最佳答案

Generic “runtest” hooks

所有与运行测试相关的 Hook 都会收到一个 pytest.Item 对象。

pytest_runtest_protocol(item, nextitem)[来源]

implements the runtest_setup/call/teardown protocol for the given test item, including capturing exceptions and calling reporting hooks.
Parameters: 

    item – test item for which the runtest protocol is performed.
    nextitem – the scheduled-to-be-next test item (or None if this is the end my friend). This argument is passed on to pytest_runtest_teardown().

Return boolean: 

True if no further hook implementations should be invoked.

pytest_runtest_setup(item)[来源]

called before pytest_runtest_call(item).

pytest_runtest_call(item)[来源]

called to execute the test item.

pytest_runtest_teardown(item, nextitem)[来源]

called after pytest_runtest_call.
Parameters: nextitem – the scheduled-to-be-next test item (None if no further test item is scheduled). This argument can be used to perform exact teardowns, i.e. calling just enough finalizers so that nextitem only needs to call setup-functions.

pytest_runtest_makereport(item, call)[来源]

return a _pytest.runner.TestReport object for the given pytest.Item and _pytest.runner.CallInfo.

为了更深入地理解,您可以在 _pytest.runner 中查看这些 Hook 的默认实现,也可能在 _pytest.pdb 中查看它们与 _pytest.capture 及其输入/输出捕获交互,以便在测试时立即进入交互式调试发生故障。

报告的 _pytest.terminal 专门使用报告 Hook 来打印有关测试运行的信息。

关于python - Pytest 插件 : Overriding pytest_runtest_call and friends,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38667429/

相关文章:

grails - Grails自己的插件错误ActiveMQ

java - Minecraft 插件 NoClassDefFoundError

python - 我怎样才能重写这个 fixture 调用,这样它就不会被直接调用?

python - pytest 包含来自命令行和装置的数据库详细信息

python - Py.test 没有名为 * 的模块

python - 字典到字符串不作为字典读回

python - 从多维列表创建字典,无需重复键

python添加2个对象或对象到列表

python - 防止观看次数被滥用的最佳方法

c++ - FireBreath 将值从一个函数传递到另一个函数并在 JavaScript 中使用该函数