Pytest插件:重写pytest_runtest_call和朋友 [英] Pytest plugin: Overriding pytest_runtest_call and friends

查看:586
本文介绍了Pytest插件:重写pytest_runtest_call和朋友的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm developing a test suite using pytest for a project of mine. Because of the nature of the project, I need to create a Pytest plugin that controls how the tests are being run; they are not run locally, but sent to a different process to run. (I know about xdist but I think it doesn't solve my problem.)

我一直在通过重写各种pytest_runtest_*方法来编写自己的Pytest插件.到目前为止,进展顺利.这是我碰壁的地方:我希望pytest_runtest_setuppytest_runtest_callpytest_runtest_teardown的实现实际上负责执行设置,调用和拆卸.他们将以不同的方式来做. 我的问题是:Pytest调用了我的pytest_runtest_setup之后,它还调用了插件行中的所有其他pytest_runtest_setup.这是因为pytest_runtest_setup的挂钩规范具有firstresult=False.

I've been writing my own Pytest plugin by overriding the various pytest_runtest_* methods. So far it's been progressing well. Here is where I've hit a wall: I want my implementations of pytest_runtest_setup, pytest_runtest_call and pytest_runtest_teardown to actually be responsible for doing the setup, call and teardown. They're going to do it in a different process. My problem is: After Pytest calls my pytest_runtest_setup, it also calls all the other pytest_runtest_setup down the line of plugins. This is because the hook specification for pytest_runtest_setup has firstresult=False.

我不想要这个,因为我不希望pytest_runtest_setup实际上在当前进程上运行.我想负责自己运行.我想改写它的运行方式,而不是添加.我希望我自己下面的pytest_runtest_setup其他实现不运行.

I don't want this, because I don't want pytest_runtest_setup to actually run on the current process. I want to be responsible for running it on my own. I want to override how it's being run, not add to it. I want the other implementations of pytest_runtest_setup below my own to not be run.

我该怎么做?

推荐答案

所有与运行测试相关的钩子都接收pytest.Item对象.

All runtest related hooks receive a pytest.Item object.

pytest_runtest_protocol(item,nextitem)[源代码]

pytest_runtest_protocol(item, nextitem)[source]

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)[源代码]

pytest_runtest_setup(item)[source]

called before pytest_runtest_call(item).

pytest_runtest_call(item)[源代码]

pytest_runtest_call(item)[source]

called to execute the test item.

pytest_runtest_teardown(项目,nextitem)[源代码]

pytest_runtest_teardown(item, nextitem)[source]

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(项目,通话)[源代码]

pytest_runtest_makereport(item, call)[source]

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

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

For deeper understanding you may look at the default implementation of these hooks in _pytest.runner and maybe also in _pytest.pdb which interacts with _pytest.capture and its input/output capturing in order to immediately drop into interactive debugging when a test failure occurs.

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

The _pytest.terminal reported specifically uses the reporting hook to print information about a test run.

这篇关于Pytest插件:重写pytest_runtest_call和朋友的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆