pytest文檔11-assert斷言

前言

斷言是寫自動化測試基本最重要的一步,一個用例沒有斷言,就失去了自動化測試的意義了。什麼是斷言呢? 簡單來說就是實際結果和指望結果去對比,符合預期那就測試pass,不符合預期那就測試 failedhtml

assert

pytest容許您使用標準Python斷言來驗證Python測試中的指望和值。例如,你能夠寫下python

# content of test_assert1.py
def f():
    return 3
def test_function():
    assert f() == 4

斷言f()函數的返回值,接下來會看到斷言失敗,由於返回的值是3,判斷等於4,因此失敗了linux

$ 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 == 4session

異常信息

接下來再看一個案例,若是想在異常的時候,輸出一些提示信息,這樣報錯後,就方便查看是什麼緣由了函數

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的值爲:3spa

異常斷言

爲了寫關於引起異常的斷言,可使用pytest.raises做爲上下文管理器,以下設計

# content of test_assert1.py

import pytest
def test_zero_division():
    with pytest.raises(ZeroDivisionError):
        1 / 0

運行結果code

============================= 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,因而用例能夠這樣設計orm

# 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

相關文章
相關標籤/搜索