自動化測試中如何判斷測試是否通過?詳解 Pytest 測試框架的斷言用法

春和景明發表於2020-05-11

軟體測試的主要工作目標是驗證實際結果與預期結果是一致的,在自動化軟體測試中,通過斷言來實現這一目的。Pytest中斷言是通過Python原生的assert語句實現的,對Python原生的assert語句進行了優化,當發生斷言失敗時,錯誤資訊更加豐富,方便測試時快速定位問題原因。

正文字數5195

保留所有權利,禁止轉載

不管是做API測試、Web測試還是APP測試中,測試用例是否執行成功,都是通過比較實際結果與預期結果是否一致來判斷的。當預期結果與實際結果一致,則表示測試用例執行通過,當預期結果與實際結果不一致,則表示測試用例執行失敗。對預期結果與實際結果進行比較的過程,在自動化軟體測試中是通過斷言來實現的。

優秀的測試框架都提供了斷言的方法,比如TestNG中的assertTrue、 assertEquals、assertSame等等。前面給大家介紹過Pytest的使用方法《基於Pytest框架的自動化測試開發實踐(萬字長文入門篇)》,本文將詳細介紹Pytest的斷言,與TestNG相比它更加簡單,只有一個assert語句,但是功能非常強大並且簡單易用。

01 — Python原生的assert

Python中assert語句通常用來對程式碼進行必要的檢查,確定某種情況一定發生,或者一定不會發生。

Python 的 assert 語句的語法是這樣的:

assert expression1 ["," expression2]

expression1往往是一個條件表示式,如果條件表示式為True,則什麼也不做,相當於執行了 pass 語句;如果條件表示式為False,便會丟擲異常 AssertionError,並返回具體的錯誤資訊expression2。看一個實際例子:

# content of my_assertion.py
def assertion():
assert [1, 2, 3] == [1, 2, 4], "left is [1,2,3], right is [1,2,4]"

if __name__ == '__main__':
assertion()

執行一下上面的程式碼看看結果:

$ python my_assertion.py 
Traceback (most recent call last):
File "my_assertion.py", line 5, in <module>
assertion()
File "my_assertion.py", line 2, in assertion
assert [1, 2, 3] == [1, 2, 4], "left is [1,2,3], right is [1,2,4]"
AssertionError: left is [1,2,3], right is [1,2,4]

可見,assert後面的條件表示式為False,丟擲了AssertionError,並顯示了錯誤資訊left is [1, 2, 3], right is [1, 2, 4]。

不過,這裡還有一點小小的缺憾。並沒有明確告訴開發人員,條件判斷失敗的具體位置。需要開發人員自己對比才發現,==左邊的第三個元素和右邊的第三個元素不一樣。

02 — Pytest的assert優點

軟體測試工作,經常會遇到斷言失敗的情況。如果每次失敗,都需要測試工程師人眼去觀察失敗的具體原因和出錯的位置,那將是非常耗時的。強大的Pytest也考慮到了廣大測試工程師面臨的問題,因此對Python原生的assert語句進行了優化和改進,主要在是當斷言失敗時,將錯誤的具體資訊和位置顯示出來,讓測試工程師對失敗原因一目瞭然。

還是上面的例子,將其放入到測試用例(test_開頭的函式)中:

# content of test_assertion.py
def test_assertion():
assert [1, 2, 3] == [1, 2, 4], "left is [1,2,3], right is [1,2,4]"
執行測試用例後的資訊輸出如下:

def test_assertion():
> assert [1, 2, 3] == [1, 2, 4], "left is [1,2,3], right is [1,2,4]"
E AssertionError: left is [1,2,3], right is [1,2,4]
E assert [1, 2, 3] == [1, 2, 4]
E At index 2 diff: 3 != 4
E Full diff:
E - [1, 2, 4]
E ? ^
E + [1, 2, 3]
E ?

是不是有種很爽的感覺?pytest明確顯示出了錯誤的位置是index為2的元素左右不相等。這一點點小小的改進大大提高了測試失敗時定位出錯原因的效率。

在測試用例中執行assert語句,才有上面的效果,這是因為Pytest對assert語句進行了重寫。在非測試用例中的assert語句,比如測試專案中的一些utils函式中,使用assert還是Python原生的效果。

03 — Pytest 斷言的用法

在自動化測試用例中,最常用的斷言是相等斷言,就是斷言預期結果和實際結果是一致的。通常我們斷言的預期結果和實際結果的資料型別是字串、元組、字典、列表和物件。Pytest通過assert和==能夠完美支援對這些資料型別的相等斷言。下面來介紹幾種常見的資料型別的斷言操作。

4.1 斷言字串

斷言字串非常簡單,只需要將預期和實際的字串,分別寫在==兩邊,當發生斷言失敗時,將會列出第一個不相等元素的下標。下面是幾個在實際測試工作中經常用到的幾種字串斷言方式。

# content of test_assertions.py
class TestAssertions(object):
def test_string_1(self):
assert "spam" == "eggs"

def test_string_2(self):
assert "foo 1 bar" == "foo 2 bar"

def test_string_3(self):
assert "foo\nspam\nbar" == "foo\neggs\nbar"

def test_string_4(self):
def f():
return "streaming"
assert f().startswith('S')

執行一下這些測試用例,看下輸出效果,核心部分如下:

============================================================ FAILURES ============================================================
__________________________________________________ TestAssertions.test_string_1 __________________________________________________

self = <test_assertions.TestAssertions object at 0x10911a4d0>

def test_string_1(self):
> assert "spam" == "eggs"
E AssertionError: assert 'spam' == 'eggs'
E - eggs
E + spam

tests/test_assertions.py:3: AssertionError
__________________________________________________ TestAssertions.test_string_2 __________________________________________________

self = <test_assertions.TestAssertions object at 0x10911a890>

def test_string_2(self):
> assert "foo 1 bar" == "foo 2 bar"
E AssertionError: assert 'foo 1 bar' == 'foo 2 bar'
E - foo 2 bar
E ? ^
E + foo 1 bar
E ? ^

tests/test_assertions.py:6: AssertionError
__________________________________________________ TestAssertions.test_string_3 __________________________________________________

self = <test_assertions.TestAssertions object at 0x10911c2d0>

def test_string_3(self):
> assert "foo\nspam\nbar" == "foo\neggs\nbar"
E AssertionError: assert 'foo\nspam\nbar' == 'foo\neggs\nbar'
E foo
E - eggs
E + spam
E bar

tests/test_assertions.py:9: AssertionError
__________________________________________________ TestAssertions.test_string_4 __________________________________________________

self = <test_assertions.TestAssertions object at 0x109106a90>

def test_string_4(self):
def f():
return "streaming"

> assert f().startswith('S')
E AssertionError: assert False
E + where False = <built-in method startswith of str object at 0x1090f7bb0>('S')
E + where <built-in method startswith of str object at 0x1090f7bb0> = 'streaming'.startswith
E + where 'streaming' = <function TestAssertions.test_string_4.<locals>.f at 0x10914b440>()

tests/test_assertions.py:15: AssertionError

再次感覺到測試結果一目瞭然。

4.2 斷言函式或者介面返回值

對函式返回值、介面返回值的斷言,應該是軟體自動化測試中最常見的場景了。這裡以函式返回值的斷言為例,

def test_function():
def f():
return [1, 2, 3]

assert f() == [1, 2, 4]

執行這個測試用例,看下輸出的錯誤資訊:

============================================================ FAILURES ============================================================
_________________________________________________________ test_function __________________________________________________________

def test_function():
def f():
return [1, 2, 3]

> assert f() == [1, 2, 4]
E assert [1, 2, 3] == [1, 2, 4]
E At index 2 diff: 3 != 4
E Full diff:
E - [1, 2, 4]
E ? ^
E + [1, 2, 3]
E ? ^

tests/test_assertions.py:22: AssertionError

可以看到,輸出資訊中包含了函式的返回值,並且顯示了返回值與預期結果不一致的元素是index為2的元素。

4.3 斷言集合型別

斷言列表、元組、字典和集合等型別在測試中也是很常見的,對於具有巢狀的集合資料,pytest的assert依然能夠精確地顯示出來出錯的位置。比如下面這段測試用例程式碼:

class TestCollections(object):
def test_dict(self):
assert {"a": 0, "b": 1, "c": 0} == {"a": 0, "b": 2, "d": 0}

def test_dict2(self):
assert {"a": 0, "b": {"c": 0}} == {"a": 0, "b": {"c": 2}}

def test_list(self):
assert [0, 1, 2] == [0, 1, 3]

def test_list2(self):
assert [0, 1, 2] == [0, 1, [1, 2]]

def test_set(self):
assert {0, 10, 11, 12} == {0, 20, 21}

執行上面的測試程式碼,核心輸出會是下面這樣:

============================================================ FAILURES ============================================================
___________________________________________________ TestCollections.test_dict ____________________________________________________

self = <test_assertions.TestCollections object at 0x10b0d2d10>

def test_dict(self):
> assert {"a": 0, "b": 1, "c": 0} == {"a": 0, "b": 2, "d": 0}
E AssertionError: assert {'a': 0, 'b': 1, 'c': 0} == {'a': 0, 'b': 2, 'd': 0}
E Omitting 1 identical items, use -vv to show
E Differing items:
E {'b': 1} != {'b': 2}
E Left contains 1 more item:
E {'c': 0}
E Right contains 1 more item:
E {'d': 0}...
E
E ...Full output truncated (6 lines hidden), use '-vv' to show

tests/test_assertions.py:27: AssertionError
___________________________________________________ TestCollections.test_dict2 ___________________________________________________

self = <test_assertions.TestCollections object at 0x10b0d2a90>

def test_dict2(self):
> assert {"a": 0, "b": {"c": 0}} == {"a": 0, "b": {"c": 2}}
E AssertionError: assert {'a': 0, 'b': {'c': 0}} == {'a': 0, 'b': {'c': 2}}
E Omitting 1 identical items, use -vv to show
E Differing items:
E {'b': {'c': 0}} != {'b': {'c': 2}}
E Full diff:
E - {'a': 0, 'b': {'c': 2}}
E ? ^
E + {'a': 0, 'b': {'c': 0}}...
E
E ...Full output truncated (2 lines hidden), use '-vv' to show

tests/test_assertions.py:30: AssertionError
___________________________________________________ TestCollections.test_list ____________________________________________________

self = <test_assertions.TestCollections object at 0x10b0c1190>

def test_list(self):
> assert [0, 1, 2] == [0, 1, 3]
E assert [0, 1, 2] == [0, 1, 3]
E At index 2 diff: 2 != 3
E Full diff:
E - [0, 1, 3]
E ? ^
E + [0, 1, 2]
E ? ^

tests/test_assertions.py:33: AssertionError
___________________________________________________ TestCollections.test_list2 ___________________________________________________

self = <test_assertions.TestCollections object at 0x10b0d6c10>

def test_list2(self):
> assert [0, 1, 2] == [0, 1, [1, 2]]
E assert [0, 1, 2] == [0, 1, [1, 2]]
E At index 2 diff: 2 != [1, 2]
E Full diff:
E - [0, 1, [1, 2]]
E ? ---- -
E + [0, 1, 2]

tests/test_assertions.py:36: AssertionError
____________________________________________________ TestCollections.test_set ____________________________________________________

self = <test_assertions.TestCollections object at 0x10b0c1a50>

def test_set(self):
> assert {0, 10, 11, 12} == {0, 20, 21}
E AssertionError: assert {0, 10, 11, 12} == {0, 20, 21}
E Extra items in the left set:
E 10
E 11
E 12
E Extra items in the right set:
E 20
E 21...
E
E ...Full output truncated (4 lines hidden), use '-vv' to show

tests/test_assertions.py:39: AssertionError

可以看到對於巢狀的字典和列表,也能顯示出不一致資料的具體位置。對於過長的資料,預設是會被truncated,可以通過-vv顯示全部資訊。

除了相等斷言,還可以進行大於、小於、不等於、in/not in等型別的斷言。

對於物件的斷言,可以進行物件的型別斷言、物件本身的斷言。這裡就不在一一舉例,只要記住斷言是使用assert語句,使用方法與在Python語言中的使用方法完全一致就可以了。

更多斷言的例子,大家可以參考Pytest的官方文件:https://docs.pytest.org/en/latest/example/reportingdemo.html44個斷言的例子,非常全面,幾乎涵蓋了所有的相等斷言的場景。,這裡一共有

04 — Pytest斷言Excepiton

除了支援對程式碼正常執行的結果斷言之外,Pytest也能夠對Exception和Warnning進行斷言,來斷定某種條件下,一定會出現某種異常或者警告。在功能測試和整合測試中,這兩類斷言用的不多,這裡簡單介紹一下。

對於異常的斷言,Pytest的語法是:with pytest.raises(異常型別),可以看下面的這個例子:

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

這個測試用例斷言運算表示式1除以0會產生ZeroDivisionError異常。除了對異常型別進行斷言,還可以對異常資訊進行斷言,比如:

def test_zero_division():
with pytest.raises(ZeroDivisionError) as excinfo:
1 / 0
assert 'division by zero' in str(excinfo.value)

這個測試用例,就斷言了excinfo.value的內容中包含division by zero這個字串,這在需要斷言具體的異常資訊時非常有用。

對於Warnning的斷言,其實與Exception的斷言的用法基本一致。這裡就不介紹了,關於更多的Exception和Warnning的斷言可以參考Pytest的官方文件https://docs.pytest.org/en/latest/assert.html#assertions-about-expected-exceptions

05 — 為斷言新增自定義功能

通過前面的介紹,感覺Pytest的assert挺完美了,又簡單又清晰。但是在實際的測試工作中,還會遇到一些實際問題,比如在斷言時,最好【自動】新增一些日誌,避免我們在測試程式碼中手動加入日誌。還有,最好能將斷言的資訊,【自動】整合到一些測試報告中,比如Allure中(關於Allure報告大家可以看之前的文章《用Pytest+Allure生成漂亮的HTML圖形化測試報告》)。這樣就能避免在每一個測試指令碼中手動寫很多重複的程式碼,從而讓我們將更多的時間和精力放到編寫測試用例上。

有了這樣的想法,接下來看看如何實現。

Pytest中提供了一個Hook函式pytest_assertrepr_compare,這個函式會在測試指令碼的assert語句執行時被呼叫。因此,可以實現這個函式,在函式中新增寫日誌和整合allure測試報告程式碼。

完整的程式碼如下所示:

# content of conftest.py
def pytest_assertrepr_compare(config, op, left, right):
left_name, right_name = inspect.stack()[7].code_context[0].lstrip().lstrip('assert').rstrip('\n').split(op)
pytest_output = assertrepr_compare(config, op, left, right)
logging.debug("{0} is\n {1}".format(left_name, left))
logging.debug("{0} is\n {1}".format(right_name, right))
with allure.step("校驗結果"):
allure.attach(str(left), left_name)
allure.attach(str(right), right_name)
return pytest_output

通過inspect獲取呼叫棧資訊,從中得到測試指令碼中assert語句中op操作符兩邊的字串名稱,在日誌和測試報告中會用到。接著執行assertrepr_compare輸出錯誤詳細資訊,這些資訊就是在執行斷言失敗時的輸出內容,pytest_assertrepr_compare函式沒有對其做任何修改。接著新增了debug日誌輸出和allure測試報告的內容,最後再將assert的錯誤資訊返回給呼叫處。

實現了這個函式後,測試指令碼不需要做任何修改,依然是直接使用assert進行斷言。但是能夠自動記錄日誌和生成allure測試報告了。

06 — 禁止Pytest的assert特性

如果不想要Pytest中的assert的效果,而是希望保持Python原生的assert效果,只需要在執行測試是指定一個選項:

--assert=plain

這樣所有測試用例中的assert都變成了Python原生的assert效果了,如果只想某一個模組保持Python原生的assert效果,那麼就在對應模組的docstring中新增PYTEST_DONT_REWRITE字串就好了,也就是在py檔案的最上面新增類似下面的docstring內容:

"""
Disable rewriting for a specific module by adding the string:
PYTEST_DONT_REWRITE
"""

不過,我想應該沒有人會這麼幹,因為Pytest的assert還是更好用一些。

07 — 總結

本文對比了Python原生的assert與Pytest中的assert的區別,詳細介紹了Pytest中assert的用法,並根據測試工作的實際需求,演示瞭如何通過pytest_assertrepr_compare這個Hook函式在斷言時增加日誌和報告輸出。希望對你有幫助。

參考資料

[1] https://morioh.com/tutorials

[2] https://docs.pytest.org/en/latest/example/reportingdemo

思維導圖版
pytest斷言詳解

相關文章