Python測試框架之pytest高階用法之跳過(Skip)及預期失敗(xFail): 處理不能成功的測試用例(四)


前置條件:

1.文件路徑:

- Test_App
- - test_abc.py
- - pytest.ini

 2.pyetst.ini配置文件內容:

[pytest]
命令行參數
addopts = -s
搜索文件名
python_files = test_*.py
 搜索的類名
python_classes = Test_*
 搜索的函數名
python_functions = test_*

 3.1 跳過測試函數

根據特定的條件,不執行標識的測試函數.
 方法:
     skipif(condition, reason=None)
 參數:
     condition:跳過的條件,必傳參數
     reason:標注原因,必傳參數
 使用方法:
     @pytest.mark.skipif(condition, reason="xxx") 

  

import pytest
class Test_ABC:
    def setup_class(self):
        print("------->setup_class")
    def teardown_class(self):
        print("------->teardown_class")
    def test_a(self):
        print("------->test_a")
        assert 1
    @pytest.mark.skipif(condition=2 > 1, reason="跳過該函數")  # 跳過測試函數test_b
    def test_b(self):
        print("------->test_b")
        assert 0

  

 

3.2

@pytest.mark.skip(reason=" ") -- 跳過執行測試函數

可傳入一個非必須參數reason表示原因

import pytest
@pytest.mark.skip(reason="no reason")
def test_01():
  print("---用例a執行---")
class Test_Case():
  @pytest.mark.skip(reason="no reason")
  def test_02(self):
    print("---用例b執行---")

  def test_03(self):
    print("---用例c執行---")

  

 

3.3

自定義@pytest.mark.skip()標簽

myskip = pytest.mark.skip() 或 myskip = pytest.mark.skipif(condition=...)

裝飾時用該變量代替標簽即可:@myskip

import pytest
# myskip = pytest.mark.skip()
myskip = pytest.mark.skipif(condition=2>1, reason="no reason")
@myskip
def test_01():
  print("---用例a執行---")
class Test_Case():
  @myskip
  def test_02(self):
    print("---用例b執行---")
  def test_03(self):
    print("---用例c執行---")

  

 

 

 3.4

通過pytest.skip()方法跳過測試函數

import pytest
def test_01():
  pytest.skip(msg="no reason")
  print("---用例a執行---")
class Test_Case():
  def test_02(self):
    pytest.skip()
    print("---用例b執行---")
  def test_03(self):
    print("---用例c執行---")

  

 

 

 3.5

跳過測試類

跳過測試類其實和跳過測試方法一樣,使用@pytest.mark.skip()和@pytest.mark.skipif()兩個標簽,用他們裝飾測試類就好啦。

根據某些條件跳過模塊中的所有測試用例如:

 pytestmark = pytest.mark.skipif(sys.platform == "win32",reason="tests for linux only")

import pytest
myskip = pytest.mark.skip(reason="no reason")
def test_01():
  print("---用例a執行---")
@myskip
class Test_Case():
  def test_02(self):
    print("---用例b執行---")
  def test_03(self):
    print("---用例c執行---")

  

 

3.6

跳過模塊

使用pytestmark(不可更改變量名)變量,讓他等於標簽即可。

import pytest
#pytestmark = pytest.mark.skip()
pytestmark = pytest.mark.skip(reason='no reason')
def test_01():
  print("---用例a執行---")
class Test_Case():
  def test_02(self):
    print("---用例b執行---")
  def test_03(self):
    print("---用例c執行---")

  

 

 

 4.1 標記為預期失敗函數

標記測試函數為失敗函數
 方法:
     xfail(condition=None, reason=None, raises=None, run=True, strict=False)
 常用參數:
     condition:預期失敗的條件,必傳參數
     reason:失敗的原因,必傳參數
 使用方法:
     @pytest.mark.xfail(condition, reason="xx")

  

import pytest
class Test_ABC:
    def setup_class(self):
        print("------->setup_class")
    def teardown_class(self):
        print("------->teardown_class")
    def test_a(self):
        print("------->test_a")
        assert 1
    @pytest.mark.xfail(2 > 1, reason="標注為預期失敗") # 標記為預期失敗函數test_b
    def test_b(self):
        print("------->test_b")
        assert 0

  x # 失敗標記

 

4.2使用xfail標記指示你希望測試失敗:

@pytest.mark.xfail
def test_function():
    ...

  

import pytest
class Test_ABC:
    def test_a(self):
        print("------->test_a")
        assert 1
    @pytest.mark.xfail
    def test_b(self):
        print("------->test_b")
        assert 0

  

將運行此測試,但在失敗時不會報告回溯。相反,終端報告會將其列在“預期失敗”(XFAIL)或“意外傳遞”(XPASS)部分中。

或者,也可以xfail在測試用例或setup函數中強制標記測試預期失敗:

def test_function():
    if not valid_config():
        pytest.xfail("failing configuration (but should work)") 
import pytest
class Test_ABC:
    def test_a(self):
        print("------->test_a")
        assert 1
    def test_b(self):
        if 2>1:
            pytest.xfail("failing configuration (but should work)")
        print("------->test_b")

  

 

 

這將無條件地制作test_function``XFAIL。請注意,pytest.xfail調用后不會執行其他代碼,與標記不同。那是因為它是通過引發已知異常在內部實​​現的。

 4.3  strict參數(默認是false)

如果strict參數設置為True, 如果出現xpass,測試套件的結果將視為失敗。:

 

@pytest.mark.xfail(strict=True)
def test_function():
    ...

  

import pytest
class Test_ABC:
    def test_a(self,b=1):
        print("------->test_a")
        assert 1
    @pytest.mark.xfail(strict=True)
    def test_b(self):
        print("------->test_b")
        assert 1

  

 

這將導致`xpass(“意外通過”)此測試的結果導致測試套件失敗。

strict參數也可以使用xfail_strict變量配置到pytest.ini文件中。:

[pytest]
xfail_strict=true

4.4 reason參數

與skipif一樣,你也可以在特定平台上標記你對失敗的期望:

 

@pytest.mark.xfail(sys.version_info >= (3,6),reason="Python3.6 API變更")
def test_function():
    ...  

4.5 raises參數

如果你想更具體地說明測試失敗的原因,可以在raises參數中指定單個異常或異常元組。

 

@pytest.mark.xfail(raises=RuntimeError)
def test_function():
    ...

4.6 忽略xfail

通過在命令行上指定:

pytest --runxfail

  可以強制運行並報告xfail標記的測試,就像它根本沒有標記一樣。這也導致pytest.xfail沒有效果。

import pytest
class Test_ABC:
    def test_a(self,b=1):
        print("------->test_a")
        assert 1
    @pytest.mark.xfail
    def test_b(self):
        print("------->test_b")
        assert 1

  

 


免責聲明!

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



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