【原文链接】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>
622

被折叠的 条评论
为什么被折叠?



