前言
pytest 使用命令行執行用例的時候,有些用例執行失敗的時候,屏幕上會出現一大堆的報錯內容,不方便快速查看是哪些用例失敗。
--tb=style
參數可以設置報錯的時候回溯打印內容,可以設置參數(auto/long/short/line/native/no)
--tb=style
pytest -h
查看命令行參數,關於 --tb=style
參數的使用方式
>pytest -h
--tb=style traceback print mode (auto/long/short/line/native/no).
style 的值可以設置6種打印模式:auto/long/short/line/native/no
--tb=no
先寫個簡單的pytest用例,讓他斷言報錯
# test_tb.py
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/
def test_01():
result = "hello"
expected = "world"
assert result == expected
命令行執行pytest test_tb.py
此時屏幕上會顯示整個用例的回溯信息
test_tb.py F [100%]
================================== FAILURES ===================================
___________________________________ test_01 ___________________________________
def test_01():
result = "hello"
expected = "world"
> assert result == expected
E AssertionError: assert 'hello' == 'world'
E - hello
E + world
test_tb.py:8: AssertionError
========================== 1 failed in 0.08 seconds ===========================
加上 --tb=no
參數執行
test_tb.py F [100%]
========================== 1 failed in 0.05 seconds ===========================
--tb=no
是不顯示報錯的回溯內容
--tb=line
line 模式使用一行輸出所有的錯誤信息
test_tb.py F [100%]
================================== FAILURES ===================================
D:\soft\kecheng202004\demo\test_tb.py:8: AssertionError: assert 'hello' == 'world'
========================== 1 failed in 0.06 seconds ===========================
--tb=short
short 模式顯示斷言報錯的位置,不顯示用例前面的代碼
test_tb.py F [100%]
================================== FAILURES ===================================
___________________________________ test_01 ___________________________________
test_tb.py:8: in test_01
assert result == expected
E AssertionError: assert 'hello' == 'world'
E - hello
E + world
========================== 1 failed in 0.06 seconds ===========================
其它
--tb=auto 有多個用例失敗的時候,只打印第一個和最后一個用例的回溯信息
--tb=long 輸出最詳細的回溯信息
--tb=short 輸入assert的一行和系統判斷內容
--tb=line 使用一行顯示錯誤信息
--tb=native 只輸出python標准庫的回溯信息
--tb=no 不顯示回溯信息