前言
pytest測試框架提供的很多鈎子(Hooks)方法方便我們對測試用例框架進行二次開發,可以根據自己的需求進行改造。
例如:鈎子方法:pytest_runtest_makereport ,可以更清晰的了解用例的執行過程,並獲取到每個用例的執行結果。
pytest_runtest_makereport方法源碼
先看下相關的源碼,在 _pytest/runner.py 文件下,可以導入之后,點進去查看:
from _pytest import runner # 對應源碼 def pytest_runtest_makereport(item, call): """ return a :py:class:`_pytest.runner.TestReport` object for the given :py:class:`pytest.Item` and :py:class:`_pytest.runner.CallInfo`. """
item 是測試用例, call 是測試步驟,具體執行過程如下:
①先執行 when=’setup’ 返回setup的執行結果。
②然后執行 when=’call’ 返回call的執行結果。
③最后執行 when=’teardown’ 返回teardown的執行結果。
第一個案例
conftest.py 文件編寫 pytest_runtest_makereport 鈎子方法,打印運行過程和運行結果。
# conftest.py import pytest @pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_runtest_makereport(item, call): print('------------------------------------') # 獲取鈎子方法的調用結果 out = yield print('用例執行結果', out) # 從鈎子方法的調用結果中獲取測試報告 report = out.get_result() print('測試報告:%s' % report) print('步驟:%s' % report.when) print('nodeid:%s' % report.nodeid) print('description:%s' % str(item.function.__doc__)) print(('運行結果: %s' % report.outcome))
test_a.py 寫一個簡單的用例:
def test_a():
'''用例描述:test_a'''
print("123")
運行結果:


結果分析:
從結果可以看到,用例的過程會經歷3個階段:
setup -> call -> teardown
每個階段會返回 Result 對象和 TestReport 對象,以及對象屬性。(setup和teardown上面的用例默認沒有,結果都是passed。)
第二個案例
給用例寫個 fixture函數 增加用例的前置和后置操作; conftest.py 如下:
import pytest @pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_runtest_makereport(item, call): print('------------------------------------') # 獲取鈎子方法的調用結果 out = yield print('用例執行結果', out) # 從鈎子方法的調用結果中獲取測試報告 report = out.get_result() print('測試報告:%s' % report) print('步驟:%s' % report.when) print('nodeid:%s' % report.nodeid) print('description:%s' % str(item.function.__doc__)) print(('運行結果: %s' % report.outcome)) @pytest.fixture(scope="session", autouse=True) def fix_a(): print("setup 前置操作") yield print("teardown 后置操作")
運行結果:


第三個案例
fixture函數 的 setup 前置函數在執行時異常,即 setup 執行結果為 failed ,則后面的 call 測試用例與 teardown 后置操作函數都不會執行。
此時的狀態是 error ,也就是代表測試用例還沒開始執行就已經異常了。(在執行前置操作函數的時候就已經異常了)
import pytest @pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_runtest_makereport(item, call): print('------------------------------------') # 獲取鈎子方法的調用結果 out = yield print('用例執行結果', out) # 從鈎子方法的調用結果中獲取測試報告 report = out.get_result() print('測試報告:%s' % report) print('步驟:%s' % report.when) print('nodeid:%s' % report.nodeid) print('description:%s' % str(item.function.__doc__)) print(('運行結果: %s' % report.outcome)) @pytest.fixture(scope="session", autouse=True) def fix_a(): print("setup 前置操作") assert 1 == 2 yield print("teardown 后置操作")
運行結果:


第四個案例
setup 前置操作函數正常執行,測試用例 call 執行出現異常。
此時的測試用例執行結果為 failed
# conftest.py
import pytest @pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_runtest_makereport(item, call): print('------------------------------------') # 獲取鈎子方法的調用結果 out = yield print('用例執行結果', out) # 3. 從鈎子方法的調用結果中獲取測試報告 report = out.get_result() print('測試報告:%s' % report) print('步驟:%s' % report.when) print('nodeid:%s' % report.nodeid) print('description:%s' % str(item.function.__doc__)) print(('運行結果: %s' % report.outcome)) @pytest.fixture(scope="session", autouse=True) def fix_a(): print("setup 前置操作") yield print("teardown 后置操作")
# test_a.py def test_a(): """用例描述:test_a""" print("123") assert 1 == 0
運行結果:


第五個案例
setup 前置操作函數正常執行,測試用例 call 正常執行, teardown 后置操作函數執行時發生異常。
測試用例正常執行,但是測試結果中會有 error ,因為 teardown 后置操作函數在執行時發生異常
# conftest.py
import pytest @pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_runtest_makereport(item, call): print('------------------------------------') # 獲取鈎子方法的調用結果 out = yield print('用例執行結果', out) # 從鈎子方法的調用結果中獲取測試報告 report = out.get_result() print('測試報告:%s' % report) print('步驟:%s' % report.when) print('nodeid:%s' % report.nodeid) print('description:%s' % str(item.function.__doc__)) print(('運行結果: %s' % report.outcome)) @pytest.fixture(scope="session", autouse=True) def fix_a(): print("setup 前置操作") yield print("teardown 后置操作") raise Exception("teardown 失敗了")
# test_a.py def test_a(): '''用例描述:test_a''' print("123")
運行結果:



第六個案例:只獲取call結果
場景:編寫測試用例時,在保證 setup 前置操作函數和 teardown 后置操作函數不報錯的前提下,只需要關注測試用例的執行結果,即只需要獲取測試用例執行call的結果。
解決辦法:因為前面的 pytest_runtest_makereport 鈎子方法執行了三次。所以在打印測試報告的相關數據之氣可以加個判斷: if report.when == “call” 。
import pytest from _pytest import runner ''' # 對應源碼 def pytest_runtest_makereport(item, call): """ return a :py:class:`_pytest.runner.TestReport` object for the given :py:class:`pytest.Item` and :py:class:`_pytest.runner.CallInfo`. """ ''' @pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_runtest_makereport(item, call): print('------------------------------------') # 獲取鈎子方法的調用結果 out = yield # print('用例執行結果:', out) # 從鈎子方法的調用結果中獲取測試報告 report = out.get_result() if report.when == "call": print('測試報告:%s' % report) print('步驟:%s' % report.when) print('nodeid:%s' % report.nodeid) print('description:%s' % str(item.function.__doc__)) print(('運行結果: %s' % report.outcome)) @pytest.fixture(scope="session", autouse=True) def fix_a(): print("setup 前置操作") yield print("teardown 后置操作")
運行結果:


conftest.py 去除pytest_runtest_makereport 鈎子方法,正常執行測試用例
# conftest.py import pytest @pytest.fixture(scope="session", autouse=True) def fix_a(): print("setup 前置操作") yield print("teardown 后置操作")
# test_a.py def test_a(): """用例描述:test_a""" print("123")
運行結果:


