對於測試來講,不管是功能測試,自動化測試,還是單元測試。一般都會預設一個正確的預期結果,而在測試執行的過程中會得到一個實際的結果。測試的成功與否就是拿實際的結果與預期的結果進行比較。這個比的過程實際就是斷言(assert)。
在unittest單元測試框架中提供了豐富的斷言方法,例如assertEqual()、assertIn()、assertTrue()、assertIs()等,而pytest單元測試框架中並沒提供特殊的斷言方法,而是直接使用python的assert進行斷言。
下面我們就來介紹assert 的使用。
比較大小與是否相等
test_assert.py
#coding=utf-8
import pytest # 功能
def add(a,b): return a + b # 測試相等
def test_add(): assert add(3,4) == 7
# 測試不相等
def test_add2(): assert add(17,22) != 50
# 測試大於
def test_add3(): assert add(17,22) <= 50
# 測試小於
def test_add4(): assert add(17,22) >= 50
if __name__ == '__main__': pytest.main("test_assert.py")
定義一個add()函數,用於計算兩個入參相加,並將相加的結果返回。
而assert可以使用直接使用“==”、“!=”、“<”、“>”、“>=”、"<=" 等符號來比較相等、不相等、小於、大於、大於等於和小於等於。
運行結果:
============================= test session starts ============================= platform win32 -- Python 2.7.10 -- py-1.4.30 -- pytest-2.7.2 rootdir: D:\pyse\pytest\test_case, inifile: plugins: html collected 4 items test_assert.py ...F ================================== FAILURES ===================================
__________________________________ test_add4 __________________________________
def test_add4(): > assert add(17,22) >= 50 E assert 39 >= 50 E + where 39 = add(17, 22) test_assert.py:22: AssertionError ===================== 1 failed, 3 passed in 0.02 seconds ======================
顯然,17加22的結果並不大於50,所有最后一條用例失敗。
測試包含或不包含
test_assert2.py
#coding=utf-8
import pytest # 測試相等
def test_in(): a = "hello" b = "he"
assert b in a # 測試不相等
def test_not_in(): a = "hello" b = "hi"
assert b not in a if __name__ == '__main__': pytest.main("test_assert2.py")
通過定義a和b 字符串變量來比較包含的關系。
assert 可以直接使用 in 和not in 來比較包含與不包含。
運行結果:
============================= test session starts ============================= platform win32 -- Python 2.7.10 -- py-1.4.30 -- pytest-2.7.2 rootdir: D:\pyse\pytest\test_case, inifile: plugins: html collected 2 items test_assert2.py F. ================================== FAILURES ===================================
___________________________________ test_in ___________________________________
def test_in(): a = "hello" b = "hi"
> assert b in a E assert 'hi' in 'hello' test_assert2.py:9: AssertionError ===================== 1 failed, 1 passed in 0.01 seconds ======================
顯然“hello”並不包含“hi”,所以第一條測試用例運行失敗。
測試true或false
test_assert3.py
#coding=utf-8
import pytest #用於判斷素數
def is_prime(n): if n <= 1: return False for i in range(2, n): if n % i == 0: return False return True # 判斷是否為素數
def test_true(): assert is_prime(13) # 判斷是否不為素數
def test_true(): assert not is_prime(7) if __name__ == '__main__': pytest.main("test_assert3.py")
通過is_prime()函數來判斷n 是否為素數(只能被1和它本身整除的數)。返回值為ture或false。
通過assert不需要任何輔助符號,直接判斷對象是否為ture,而assert not 用於判斷是否為false。
運行結果:
============================= test session starts ============================= platform win32 -- Python 2.7.10 -- py-1.4.30 -- pytest-2.7.2 rootdir: D:\pyse\pytest\test_case, inifile: plugins: html collected 1 items test_assert3.py F ================================== FAILURES ===================================
__________________________________ test_true __________________________________
def test_true(): > assert not is_prime(7) E assert not True E + where True = is_prime(7) test_assert3.py:22: AssertionError ========================== 1 failed in 0.01 seconds ===========================
顯示,對於第二條測試用例來講,7是素數,所以,is_prime()函數的返回結果是Ture,而assert not 需要的正確結果是False,因此,用例執行失敗。