1. 程式人生 > 其它 >pytest -mark skip&xfail

pytest -mark skip&xfail

1. skip跳過

自動化測試過程中,可能會遇到環境問題、已知bug、未完成介面等情況需要跳過用例,這時就需要用到pytest.mark.skip()或pytest.mark.skipif()

@pytest.mark.skip(reason)  只要有此標記就跳過,無條件

@pytest.mark.skipif(condition,reason="跳過原因")  滿足輸入的條件,就跳過

適用範圍:

  • 放在測試類前面,跳過該類下的所有用例
  • 放在方法或者測試用例前面,跳過此方法或測試用例

 直接跳過 可加引數reason=“跳過原因”

    @pytest.mark.skip(reason="跳過該用例")
    @pytest.mark.parametrize([
"a","b","c"],[(1,2,3),(3,4,7)]) def test_list(self,a,b,c): assert a+b == c print(a+b)

滿足條件跳過,即condition返回值為true

    @pytest.mark.parametrize(["a","b","c"],[(1,2,3),(3,4,7)])
    @pytest.mark.skipif(1==1,reason = "跳過該用例")
    def test_list(self,a,b,c):
        assert a+b == c
        
print(a+b)

將skip賦值給變數,可多處呼叫

myskip = pytest.mark.skip(reason="跳過a,b相關用例")
class Test_param:
    @myskip
    def test_param(self,a=1,b=2):
        assert a+b == 4
        print(a+b)

    #測試單個引數
    @myskip
    @pytest.mark.parametrize("a",["test123456","test2222"])
    def test_a(self,a):
        
print(a) #測試多個引數 @myskip @pytest.mark.parametrize("a,b",[(1,2),(3,4)]) def test_two_param(self,a,b): print(a+b)

2. xfail 該用例置為失敗

已經知道該用例結果是失敗的,又不想跳過,可以用xfail

xfail不會影響測試用例的執行,如果執行成功則報xpass,如果失敗就會報xfail

  #執行結果為xpass
  @pytest.mark.xfail()
    def test_normal(self,c=10):
        print(c)

    #執行結果為xfail
    @pytest.mark.xfail()
    @pytest.mark.parametrize(["a","b","c"],[(1,2,3),(3,4,7)])
    def test_list(self,a,b,c):
        assert a+b != c
        print(a+b)

用例中標記失敗,用pytest.xfail標記的測試用例,執行到這一句時,直接置為xfail,沒有xpass

    data1 = {
    "test1":"test1",
    "test2":"test2"
    }
    @pytest.mark.parametrize("test",data1.items())
    def test_json(self,test):
        pytest.xfail(reason='該功能未完成')
        print(test)