Pytest----pytest-tldr 插件简化脚本执行日志输出

【原文链接】Pytest----pytest-tldr 插件简化脚本执行日志输出

Pytest测试框架在执行的时候多实时标准输出、实时日志、捕获日志、捕获标准输出等多种类型的内容在控制台显示,让许多人觉得输出不是太友好,尤其是当用例失败后,来回上下翻控制台的输出却总感觉找不到报错位置。而pytest-tldr插件则就是为了解决这个问题的,让用例报错后仅仅显示报错用例的调用错误栈,而不去显示各种错综复杂的乱七八糟的显示。
为了更好的体验pytest-tldr插件的效果,在安装pytest-tldr之前,首先利用如下用例执行一次观察一线报错信息。

def test_1():
    print("in test_1")
    assert 1==1

def test_2():
    print("in test_2")
    assert 1==2

执行结果如下,可以看到其实本来这个用例非常的简单,仅仅是直接判断1和2是否相等的断言,但是经pytest执行后打印出来的信息确实这么多,让人尤其是新手会感觉无法入手定位问题的感觉。

(demo-HCIhX0Hq) E:\demo>pytest
Test session starts (platform: win32, Python 3.7.9, pytest 7.2.0, pytest-sugar 0.9.6)
benchmark: 4.0.0 (defaults: timer=time.perf_counter disable_gc=False min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10 warmup=False warmup_iterations=100000)
rootdir: E:\demo, configfile: pytest.ini
plugins: allure-pytest-2.12.0, assume-2.4.3, attrib-0.1.3, benchmark-4.0.0, csv-3.0.0, rerunfailures-10.2, sugar-0.9.6
collecting ...
 test_demo.py ✓                               50% █████

――――――――――――――――――――――――― test_2 ――――――――――――――――――――――――――

    def test_2():
        print("in test_2")
>       assert 1==2
E       assert 1 == 2

test_demo.py:8: AssertionError
------------------ Captured stdout call -------------------
in test_2

 test_demo.py ⨯                              100% ██████████
================= short test summary info =================
FAILED test_demo.py::test_2 - assert 1 == 2

Results (0.15s):
       1 passed
       1 failed
         - test_demo.py:6 test_2

(demo-HCIhX0Hq) E:\demo>

然后通过如下命令安装pytest-tldr插件。

pip install pytest-tldr

安装完成后,此时再次使用pytest执行,如下可以看出此时的结构就简单多了,很明了。

(demo-HCIhX0Hq) E:\demo>pytest
.F
==============================================================================
FAIL: test_demo.py::test_2
------------------------------------------------------------------------------
in test_2

Traceback (most recent call last):
  File "E:\demo\test_demo.py", line 8, in test_2
    assert 1==2
AssertionError: assert 1 == 2

------------------------------------------------------------------------------
Ran 2 tests in 0.08s

FAILED (failures=1)

(demo-HCIhX0Hq) E:\demo>

下面将用例断言改为成功,如下。

def test_1():
    print("in test_1")
    assert 1==1

def test_2():
    print("in test_2")
    assert 1==1

如下,关闭tldr插件执行结果如下。

(demo-HCIhX0Hq) E:\demo>pytest -p no:tldr
Test session starts (platform: win32, Python 3.7.9, pytest 7.2.0, pytest-sugar 0.9.6)
benchmark: 4.0.0 (defaults: timer=time.perf_counter disable_gc=False min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10 warmup=False warmup_iterations=100000)
rootdir: E:\demo, configfile: pytest.ini
plugins: allure-pytest-2.12.0, assume-2.4.3, attrib-0.1.3, benchmark-4.0.0, csv-3.0.0, rerunfailures-10.2, sugar-0.9.6
collecting ...
 test_demo.py ✓✓                             100% ██████████

Results (0.09s):
       2 passed

(demo-HCIhX0Hq) E:\demo>

而放开tldr插件后执行结果如下所示,可以发现此时显示非常的简洁。

(demo-HCIhX0Hq) E:\demo>pytest
..
------------------------------------------------------------------------------
Ran 2 tests in 0.08s

OK

(demo-HCIhX0Hq) E:\demo>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

redrose2100

您的鼓励是我最大的创作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值