1. 程式人生 > 實用技巧 >pytest---pytest斷言(assert)

pytest---pytest斷言(assert)

前言

  我們在寫自動化的過程中,用例的斷言也是至關重要的,斷言可以幫助我們判斷用例測試點是否成功和失敗。當然在我們這麼強大的pytest框架中,斷言也是比較強大的。為什麼?繼續往下看

pytest斷言

前面說到pytest的斷言比較強大,它直接可以使用python自帶的斷言內容,當然不止而已,pytest還有一個重要的功能是可以重寫assert關鍵字,pytest會截斷對python中自帶的assert的呼叫然後替換成自己定位的assert,從而可以獲取更多的錯誤資訊,讓我們知道具體哪裡出現了問題。

編寫一個加法進行通過斷言驗證

import pytest

class Test_01:

    
def add(self,x,y): c = x + y return c def test_001(self): c = self.add(1,2) assert c == 4 if __name__ == '__main__': pytest.main(['-s'])

看到上面的程式碼大家都知道是會失敗的, 那麼我們一起看下pytest斷言會給我們增加哪些新的報錯資訊。

從上圖的結果中可以看出帶“>”的是python自帶的錯誤解釋,而帶E的則是pytest進行新增的,這樣就能很清楚的看到錯誤資訊,比如這個3==4結果是錯誤的。

增加斷言詳細資訊

在編寫用例的時候,斷言的時候想要獲取一些更加有用的資訊,比如引數化的時候,需要知道哪個引數進行傳入的,如果出錯了可以幫助我們進行除錯,那麼這一點pytest中的assert也可以幫助我們進行增加

import pytest

class Test_01:

    def add(self,x,y):
            c = x + y
            return c
def test_001(self): a = 1 b = 2 assert self.add(a,b) == 4 , '當前傳入的a值:%s,傳入的b值:%s
' %(a,b)
if __name__ == '__main__': pytest.main(['-s'])

很清楚的看到了python自帶的assert和pytest增加的對比,python自帶的沒有把值傳入,pytest幫助我們把值傳入進去,更加清楚的幫助檢視錯誤資訊

異常斷言

pytest斷言方法內容應該沒有unittests多,但是比unittest更加方便,更加靈活。這裡安靜在舉一個栗子。比如我們在執行一條錯誤的用例的時候,已經知道錯誤型別,這個時候我們也可以通過判斷這個錯誤型別,如果錯誤型別和預期一致,那麼就直接通過用例,反之失敗。

安靜先寫一個錯誤的函式,然後找到他的錯誤型別,下面的函式錯誤型別為"AssertionError"

def cake():
        a = 'anjing'
        b = 'test_anjing'
        assert  a == b
cake()
------------執行結果--------------

 File "E:/auto_test/test_01.py", line 5, in <module>
    cake()
  File "E:/auto_test/test_01.py", line 4, in cake
    assert  a == b
AssertionError

這裡可以使用pytest.raises(錯誤型別) 進行對比斷言

import pytest

class Test_01:

    def cake(self):
        a = 'anjing'
        b = 'test_anjing'
        assert  a == b

    def test_001(self):
        print('Test_01下的用例001')
        with pytest.raises(AssertionError):
            self.cake()

if __name__ == '__main__':
    pytest.main(['-v'])

這個從表面上執行結果肯定是失敗的,因為a不等於b,但是加入pytest的斷言執行後就成功了

上文中的異常斷言,可以進行例項化,例項化過後存在三個可以用的方法,分別是.type,.value,.traceback安靜簡單的進行舉例說明

.type

我們這裡也可以通過pytest.raises進行斷言,我們可以根據返回的tpye進行對比,檢視我們預期的與實際的是否一致

import pytest

class Test_01:

    def cake(self):
        a = 'anjing'
        b = 'test_anjing'
        assert  a == b

    def test_001(self):
        print('Test_01下的用例001')
        with pytest.raises(AssertionError) as excinfo:
            self.cake()
        print('錯誤的型別是:%s' %excinfo.type)
        assert excinfo.type == AssertionError

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

.value

如果還不知道具體的錯誤出現在哪裡,我們也可以通過value進行檢視具體的錯誤型別,

import pytest

class Test_01:

    def cake(self):
        a = 'anjing'
        b = 'test_anjing'
        assert  a == b

    def test_001(self):
        print('Test_01下的用例001')
        with pytest.raises(ZeroDivisionError) as excinfo:
            self.cake()
        print('錯誤的型別是:%s' %excinfo.type)
        print('錯誤的值:%s'%excinfo.value)
        assert excinfo.type == ZeroDivisionError

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

可以看出具體錯誤的值是assert中的兩個字串不相等導致的。

.traceback

這裡這個方法表示可以把程式碼中哪行出現了錯誤進行打印出來,此方法必須在str下進行列印。

pytest常用斷言

前面也介紹了pytest的斷言可以複用python的斷言,安靜簡單的列舉幾個python的斷言

1、判斷是否為真:assert xx

2、判斷不為真:assert not xx

3、判斷a是否包含b:assert a in b

4、判斷a不包含b:assert a not in b

5、判斷兩值相等:assert b == a

6、判斷兩值不相等:assert a != b

安靜對常用斷言簡單的舉幾個例子。

import pytest

class Test:

    def is_true(self):
        '''返回True'''
        return True

    def test_01(self):
        '''判斷是否為真'''
        assert self.is_true()

    def is_false(self):
        '''返回False'''
        return False

    def test_02(self):
        '''判斷不為真'''
        assert not self.is_false()

    def test_03(self):
        '''判斷是否相等'''
        a = 'test_anjing'
        b = 'test_anjing'
        assert a == b

    def test_04(self):
        '''判斷兩值不相等'''
        a = 'test_anjing'
        b = 'anjing'
        assert a != b

    def test_05(self):
        '''判斷a包含b'''
        a = 'test_anjing'
        b = 'anjing'
        assert b in a

    def test_06(self):
        '''判斷a不包含b'''
        a = 'anjing'
        b = 'test'
        assert b not in a

if __name__ == '__main__':
    pytest.main(['-v'])