2017-10-20 141 views
2

問題是我給定的fixture函數有一個外部依賴關係,導致一個「錯誤」(如無法訪問的網絡/資源不足等)。有沒有辦法跳過pytest夾具?

我想跳過夾具,然後跳過依賴此夾具的任何測試。

做這樣的事情不會工作:

import pytest 

@pytest.mark.skip(reason="Something.") 
@pytest.fixture(scope="module") 
def parametrized_username(): 
    raise Exception("foobar") 
    return 'overridden-username' 

這將導致

_______________________________ ERROR at setup of test_username _______________________________ 

    @pytest.mark.skip(reason="Something.") 
    @pytest.fixture(scope="module") 
    def parametrized_username(): 
>  raise Exception("foobar") 
E  Exception: foobar 

a2.py:6: Exception 

什麼是客場跳過pytest夾具的權利?

+0

你可以在'try/except'塊中填入定義嗎? –

+0

@PaulH - 測試將失敗。那麼我該如何跳過測試? –

+0

我想你將不得不單獨標記測試或者測試類裏面的東西,並且一舉跳過 –

回答

1

是的,你可以很容易地做到這一點:

import pytest 

@pytest.fixture 
def myfixture(): 
    pytest.skip('Because I want so') 

def test_me(myfixture): 
    pass 

$ pytest -v -s -ra r.py 
r.py::test_me SKIPPED 
=========== short test summary info =========== 
SKIP [1] .../r.py:6: Because I want so 

=========== 1 skipped in 0.01 seconds =========== 

內部,pytest.skip()函數拋出一個異常Skipped,這是從OutcomeException繼承。這些例外情況專門用於模擬測試結果,但不能通過測試(類似於pytest.fail())。

+1

謝謝你的教育,@Sergey。 –

相關問題