2016-07-29 30 views
7

我正在開發一個測試套件,使用pytest爲我的一個項目。由於項目的性質,我需要創建一個Pytest插件來控制測試的運行方式;它們不是在本地運行,而是發送到不同的進程運行。 (我知道xdist,但我認爲它不能解決我的問題。)Pytest插件:覆蓋pytest_runtest_call和朋友

我一直在寫我自己的Pytest插件,覆蓋各種pytest_runtest_*方法。迄今爲止進展順利。這裏是我碰壁的地方:我希望我的pytest_runtest_setup,pytest_runtest_callpytest_runtest_teardown的實現實際上負責設置,調用和拆卸。他們將在不同的過程中做到這一點。 我的問題是: Pytest調用我的pytest_runtest_setup後,它也調用插件的所有其他pytest_runtest_setup。這是因爲pytest_runtest_setup的掛鉤規格有firstresult=False

我不想要這樣,因爲我不想pytest_runtest_setup實際上在當前進程上運行。我想負責自己運行它。我想覆蓋它如何運行,而不是它。我希望pytest_runtest_setup的其他實現低於我自己的而不是被運行。

我該怎麼做?

+2

看起來你需要在pytest上修改一些模糊的函數。試圖看一下代碼,並且在調用該函數時看起來並不明顯。不過,也許有一種比monkeypatching更清潔,更兼容的解決方案。 – Hector

+0

在賞金到期之前,我沒有時間想出一個工作示例,但看起來像這樣做的鉤子是:['pytest_runtest_protocol'](http://doc.pytest.org/en/latest/ writing_plugins.html#_pytest.hookspec.pytest_runtest_protocol) –

回答

0

Generic 「runtest」 hooks

所有的runTest相關鉤收到pytest.Item對象。

pytest_runtest_protocol(項目,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(項目)[來源]

called before pytest_runtest_call(item). 

pytest_runtest_call(項目)[來源]

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(項目,調用)【來源】

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

爲了更深入的瞭解,你可以看看_pytest.runner這些掛鉤的默認實現,也許還_pytest.pdb與_pytest交互。捕獲和其輸入/輸出捕獲,以便在發生測試故障時立即進入交互式調試。

報告的_pytest.terminal特別使用報告掛鉤來打印有關測試運行的信息。