前言:斷言是自動化最終的目的,一個用例沒有斷言,就失去了自動化測試的意義了。
斷言用到的是 assert關鍵字。之前的介紹,有的測試方法中其實用到了assert斷言。簡單的來說,就是預期的結果去和實際結果做對比,符合預期結果就是pass,不符合就fail。
比如以下的一段代碼:
def add(a,b): return a+b def test_assert(): assert 6 == add(3,4)
結果:可以看出,判斷的結果是6,和方法返回的結果不一致,所以導致失敗。從黃色部分的報錯信息可以看出,結果不一致。
test_assert.py F
test_assert.py:11 (test_assert)
7 != 6
Expected :6
Actual :7
<Click to see difference>
def test_assert():
> assert 6 == add(3,4)
E assert 6 == 7
E + where 7 = add(3, 4)
test_assert.py:13: AssertionError
[100%]
=================================== FAILURES ===================================
_________________________________ test_assert __________________________________
def test_assert():
> assert 6 == add(3,4)
E assert 6 == 7
E + where 7 = add(3, 4)
test_assert.py:13: AssertionError
異常信息提示:如果想在異常的時候輸出一些提示信息,這樣報錯后就方便查看是什么原因了。
def add(a,b): return a+b def test_assert(): assert 6 == add(3,4),"方法返回的值不等於6,而是等於{0}".format(add(3,4))
結果:可以看出黃色部分,這樣會寫清楚失敗的原因,當有多個判斷時,可以容易定位問題。
test_assert.py F
test_assert.py:12 (test_assert)
def test_assert():
> assert 6 == add(3,4),"方法返回的值不等於6,而是等於{0}".format(add(3,4))
E AssertionError: 方法返回的值不等於6,而是等於7
E assert 6 == 7
E + where 7 = add(3, 4)
test_assert.py:15: AssertionError
[100%]
=================================== FAILURES ===================================
_________________________________ test_assert __________________________________
def test_assert():
> assert 6 == add(3,4),"方法返回的值不等於6,而是等於{0}".format(add(3,4))
E AssertionError: 方法返回的值不等於6,而是等於7
E assert 6 == 7
E + where 7 = add(3, 4)
test_assert.py:15: AssertionError
異常斷言:為了寫關於引發異常的斷言,可以使用pytest.raises作為上下文管理器:
import pytest def test_zero_division(): with pytest.raises(ZeroDivisionError): 1/0
結果:可以看出並沒有報錯,正常情況下,不加異常是會報錯的。
============================= test session starts ==============================
platform darwin -- Python 2.7.10, pytest-3.6.3, py-1.5.2, pluggy-0.6.0
rootdir: /Users/newcomer/PycharmProjects/error/wuya/pytestDemo, inifile:
plugins: metadata-1.7.0, html-1.19.0, D3-2.0.13, cov-2.5.1, catchlog-1.2.2, allure-adaptor-1.7.10, georaven-17.1.0.170collected 1 item
test_assert.py . [100%]
-- Docs: http://doc.pytest.org/en/latest/warnings.html
===================== 1 passed, 1 warnings in 0.01 seconds =====================
以上,我們要斷言它拋的異常是不是預期的,比如執行:1/0,預期結果是拋異常:ZeroDivisionError: division by zero,那我們要斷言這個異常。通常是斷言異常的type和value的值。這里1/0的異常類型是ZeroDivisionError,異常的value值是"integer division or modulo by zero",於是以下是代碼的設計用例:
import pytest def test_zero_division(): with pytest.raises(ZeroDivisionError,message="Exceptions ZeroDivisionError") as exinfo: 1/0 assert exinfo.type == ZeroDivisionError assert str(exinfo.value) == "integer division or modulo by zero","{0}".format(exinfo.value)
結果:可以看出來,通過了一個測試用例,但是需要注意,需要把value的值轉換成str類型的才可以比較,而且值是要加引號的。斷言type的時候,不需要給異常類型添加引號。
raises里面的message關鍵字參數消息指定自定義失敗消息。
============================= test session starts ==============================
platform darwin -- Python 2.7.10, pytest-3.6.3, py-1.5.2, pluggy-0.6.0
rootdir: /Users/newcomer/PycharmProjects/error/wuya/pytestDemo, inifile:
plugins: metadata-1.7.0, html-1.19.0, D3-2.0.13, cov-2.5.1, catchlog-1.2.2, allure-adaptor-1.7.10, georaven-17.1.0.170collected 1 item
test_assert.py .exinfo.type: <type 'exceptions.ZeroDivisionError'>
exinfo.value: integer division or modulo by zero
[100%]
===================== 1 passed, 1 warnings in 0.01 seconds =====================
import pytest def test_exception(): with pytest.raises(ZeroDivisionError,message="Exception ZeroDivisionError") as exinfo: pass
結果:可以看出來,結果中的異常是message自己定義的信息
=================================== FAILURES ===================================
________________________________ test_exception ________________________________
def test_exception():
with pytest.raises(ZeroDivisionError,message="Exception ZeroDivisionError") as exinfo:
> pass
E Failed: Exception ZeroDivisionError
test_assert.py:28: Failed
================ 1 failed, 1 warnings in 0.08 seconds ================
常用斷言:pytest里面的斷言實際上就是python里面assert的斷言方法,常用以下幾種:
·assert xx 判斷xx為真
·assert not xx 判斷xx不為真
·assert a in b 判斷b包含a
·assert a == b 判斷a等於b
·assert a != b 判斷a不等於b
def is_true(num): if num>0: return True else: return False def test_01(): """判斷是不是為真""" a = 5 b = -1 assert is_true(a) assert not is_true(b) def test_02(): """判斷b包含a""" a = "hello" b = "hello world" assert a in b def test_03(): """判斷是否相等""" a = "hello" b = "hello" c = "hello world" assert a == b assert a != c
結果:由此可以看出來,三個用例都執行成功。
============================= test session starts ==============================
platform darwin -- Python 2.7.10, pytest-3.6.3, py-1.5.2, pluggy-0.6.0
rootdir: /Users/newcomer/PycharmProjects/error/wuya/pytestDemo, inifile:
plugins: metadata-1.7.0, html-1.19.0, D3-2.0.13, cov-2.5.1, catchlog-1.2.2, allure-adaptor-1.7.10, georaven-17.1.0.170collected 3 items
test_assert.py ... [100%]
=============================== warnings summary ===============================
<undetermined location>
pytest-catchlog plugin has been merged into the core, please remove it from your requirements.
-- Docs: http://doc.pytest.org/en/latest/warnings.html
===================== 3 passed, 1 warnings in 0.02 seconds =====================