pytest命令行参数:tb的使用

本文详细介绍了如何使用pytest的--tb=style参数来优化报错信息的显示方式,包括no、line、short、auto、long和native六种模式,帮助开发者更高效地定位和解决问题。

转载自品略图书馆 http://www.pinlue.com/article/2020/09/0312/5711179354155.html

 

前言

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用例,让他断言报错



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 不显示回溯信息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值