Pytest插件:覆盖pytest_runtest_call和朋友

Ram*_*hum 8 python plugins pytest

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

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

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

我怎样才能做到这一点?

Yog*_*thi 0

通用 \xe2\x80\x9cruntest\xe2\x80\x9d 挂钩

\n\n

所有与 runtest 相关的钩子都会接收一个 pytest.Item 对象。

\n\n

pytest_runtest_protocol(item, nextitem)[来源]

\n\n
implements the runtest_setup/call/teardown protocol for the given test item, including capturing exceptions and calling reporting hooks.\nParameters: \n\n    item \xe2\x80\x93 test item for which the runtest protocol is performed.\n    nextitem \xe2\x80\x93 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().\n\nReturn boolean: \n\nTrue if no further hook implementations should be invoked.\n
Run Code Online (Sandbox Code Playgroud)\n\n

pytest_runtest_setup(项目)[来源]

\n\n
called before pytest_runtest_call(item).\n
Run Code Online (Sandbox Code Playgroud)\n\n

pytest_runtest_call(项目)[来源]

\n\n
called to execute the test item.\n
Run Code Online (Sandbox Code Playgroud)\n\n

pytest_runtest_teardown(item, nextitem)[来源]

\n\n
called after pytest_runtest_call.\nParameters: nextitem \xe2\x80\x93 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.\n
Run Code Online (Sandbox Code Playgroud)\n\n

pytest_runtest_makereport(项目,调用)[来源]

\n\n
return a _pytest.runner.TestReport object for the given pytest.Item and _pytest.runner.CallInfo.\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

报告的 _pytest.terminal 特别使用报告挂钩来打印有关测试运行的信息。

\n