pytest標記跳過某些測試用例不執行


無條件跳過(@pytets.mark.skip)

舉例:

# file_name: test_skip.py


import pytest


class Test_B:

    def test_a(self):
        print('\n------------------> test_a has ran')
        assert 1

    @pytest.mark.skip(reason="由於某種原因這個測試用例暫時不執行")
    def test_b(self):
        print('------------------> test_b has ran')
        assert 0


if __name__ == '__main__':
    pytest.main(['-s', 'test_skip.py'])

使用裝飾器@pytest.mark.skip來標記測試用例test_b,在執行過程中跳過test_b不執行。參數reason為可選參數,表示跳過的原因是什么。

有條件跳過(@pytest.mark.skipif)

根據特定的條件,不執行標識的測試函數.

 方法:
     skipif(condition, reason=None)

 參數:
     condition:跳過的條件,必傳參數
     reason:標注原因,必傳參數

 使用方法:
     @pytest.mark.skipif(condition, reason="xxx") 

舉例:

# file_name: test_skip.py


import pytest


class Test_B:

    def test_a(self):
        print('\n------------------> test_a has ran')
        assert 1

    @pytest.mark.skipif(condition=2 > 1, reason='不想執行了')
    def test_c(self):
        print("-------------------> test_c has ran")
        assert 0


if __name__ == '__main__':
    pytest.main(['-s', 'test_skip.py'])

上面的例子中,但參數condition為True時就會跳過test_c,如果condition為False則仍然會執行test_c

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM