對于測試來講,不管是功能測試,自動化測試,還是單元測試。一般都會預設一個正確的預期結果,而在測試執(zhí)行的過程中會得到一個實際的結果。測試的成功與否就是拿實際的結果與預期的結果進行比較。這個比的過程實際就是斷言(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()函數(shù),用于計算兩個入?yún)⑾嗉?,并將相加的結果返回。
而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
#用于判斷素數(shù)
def is_prime(n):
if n <= 1:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
# 判斷是否為素數(shù)
def test_true():
assert is_prime(13)
# 判斷是否不為素數(shù)
def test_true():
assert not is_prime(7)
if __name__ == '__main__':
pytest.main("test_assert3.py")
? 通過is_prime()函數(shù)來判斷n 是否為素數(shù)(只能被1和它本身整除的數(shù))。返回值為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是素數(shù),所以,is_prime()函數(shù)的返回結果是Ture,而assert?not?需要的正確結果是False,因此,用例執(zhí)行失敗。
加油吧,測試人!如果你需要提升規(guī)劃,那就行動吧,在路上總比在起點觀望的要好。未來的你肯定會感謝現(xiàn)在拼命的自己!文章來源:http://www.zghlxwxcb.cn/news/detail-510708.html
??文章來源地址http://www.zghlxwxcb.cn/news/detail-510708.html
到了這里,關于Python單元測試框架之pytest -- 斷言的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!