前言
斷言是寫自動化測試基本最重要的一步,一個用例沒有斷言,就失去了自動化測試的意義了。什么是斷言呢?
簡單來講就是實際結果和期望結果去對比,符合預期那就測試pass,不符合預期那就測試 failed
assert
pytest允許您使用標准Python斷言來驗證Python測試中的期望和值。例如,你可以寫下
# content of test_assert1.py
def f():
return 3
def test_function():
assert f() == 4
斷言f()函數的返回值,接下來會看到斷言失敗,因為返回的值是3,判斷等於4,所以失敗了
$ pytest test_assert1.py
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y
rootdir: $REGENDOC_TMPDIR, inifile:
collected 1 item
test_assert1.py F [100%]
================================= FAILURES =================================
______________________________ test_function _______________________________
def test_function():
> assert f() == 4
E assert 3 == 4
E + where 3 = f()
test_assert1.py:5: AssertionError
========================= 1 failed in 0.12 seconds =========================
從報錯信息可以看到斷言失敗原因:E assert 3 == 4
異常信息
接下來再看一個案例,如果想在異常的時候,輸出一些提示信息,這樣報錯后,就方便查看是什么原因了
def f():
return 3
def test_function():
a = f()
assert a % 2 == 0, "判斷a為偶數,當前a的值為:%s"%a
運行結果
================================== FAILURES ===================================
________________________________ test_function ________________________________
def test_function():
a = f()
> assert a % 2 == 0, "判斷a為偶數,當前a的值為:%s"%a
E AssertionError: 判斷a為偶數,當前a的值為:3
E assert (3 % 2) == 0
test_03.py:9: AssertionError
========================== 1 failed in 0.18 seconds ===========================
這樣當斷言失敗的時候,會給出自己寫的失敗原因了E AssertionError: 判斷a為偶數,當前a的值為:3
異常斷言
為了寫關於引發異常的斷言,可以使用pytest.raises作為上下文管理器,如下
# content of test_assert1.py
import pytest
def test_zero_division():
with pytest.raises(ZeroDivisionError):
1 / 0
運行結果
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: D:\YOYO\canshuhua, inifile:
plugins: metadata-1.7.0, html-1.19.0
collected 1 item
test_assert1.py.
========================== 1 passed in 0.31 seconds ===========================
如果我們要斷言它拋的異常是不是預期的,比如執行:1/0,預期結果是拋異常:ZeroDivisionError: division by zero,那我們要斷言這個異常,通常是斷言異常的type和value值了。
這里1/0的異常類型是ZeroDivisionError,異常的value值是division by zero,於是用例可以這樣設計
# content of test_assert1.py
# ** 作者:上海-悠悠 QQ交流群:588402570**
import pytest
def test_zero_division():
'''斷言異常'''
with pytest.raises(ZeroDivisionError) as excinfo:
1 / 0
# 斷言異常類型type
assert excinfo.type == ZeroDivisionError
# 斷言異常value值
assert "division by zero" in str(excinfo.value)
excinfo 是一個異常信息實例,它是圍繞實際引發的異常的包裝器。主要屬性是.type、 .value 和 .traceback
注意:斷言type的時候,異常類型是不需要加引號的,斷言value值的時候需轉str
在上下文管理器窗體中,可以使用關鍵字參數消息指定自定義失敗消息:
with pytest.raises(ZeroDivisionError, message="Expecting ZeroDivisionError"):
pass
結果:Failed: Expecting ZeroDivisionError
常用斷言
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
import pytest
# ** 作者:上海-悠悠 QQ交流群:588402570**
def is_true(a):
if a > 0:
return True
else:
return False
def test_01():
'''斷言xx為真'''
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 = "yoyo"
b = "yoyo"
assert a == b
def test_04():
'''斷言不等於'''
a = 5
b = 6
assert a != b
if __name__ == "__main__":
pytest.main(["-s", "test_01.py"])
---------------------------------pytest結合selenium自動化完整版-------------------------
全書購買地址 https://yuedu.baidu.com/ebook/902224ab27fff705cc1755270722192e4536582b
作者:上海-悠悠 QQ交流群:874033608
也可以關注下我的個人公眾號:yoyoketang

