pytest-sugar 执行过程中显示进度条的脚本分享

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

pytest-sugar 执行过程中显示进度条的脚本分享

redrose2100   2022-12-07 我要评论

Pytest-sugar是一款用来改善控制台显示的插件,增加了进度条显示,使得在用例执行过程中可以看到进度条,而且进度条是根据用例是否通过标注不同颜色,用例通过标记为绿色,用例失败则标记为红色,非常醒目。
首先使用如下命令安装pytest-sugar插件

pip install pytest-sugar

然后准备比如如下测试脚本。

def test_1():
    print("in test_1")
    assert 1==1
def test_2():
    print("in test_2")
    assert 1==1
def test_3():
    print("in test_3")
    assert 1==1
def test_4():
    print("in test_4")
    assert 1==1
def test_5():
    print("in test_5")
    assert 1==1
def test_6():
    print("in test_6")
    assert 1==1

执行pytest命令结果如下,可以发现在test_demo.py 后面跟着打印了若干个对号,同时又一段条形图,在控制台根据用例是否通过显示为绿色或者红色。

(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, rerunfailures-10.2, sugar-0.9.6
collecting ...
 test_demo.py ✓✓✓✓✓✓                         100% ██████████

Results (0.10s):
       6 passed

(demo-HCIhX0Hq) E:\demo>

当安装了pytest-sugar后默认情况下就是开启的,若不想使用pytest-sugar了可以直接卸载或者使用如下命令,也可以禁止使用pytest-sugar。如下所示。

(demo-HCIhX0Hq) E:\demo>pytest -p no:sugar
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
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, rerunfailures-10.2
collected 6 items

test_demo.py ......                                  [100%]

==================== 6 passed in 0.09s ====================

(demo-HCIhX0Hq) E:\demo>

补充知识:

pytest-sugar使用

无他,主要是安装了pytest-sugar之后,使用pytest运行代码,风格依然没有变化,在此记录一下

1. 安装pytest-sugar

pip install pytest-sugar

2. 运行

安装完成后,通过pytest运行代码,sugar不生效,查看pytest-sugar官方文档: http://pypi.org/project/pytest-sugar/

发现pytest-sugar需要有其他依赖(共3个):

  • Python 2.7, 3.4 or newer

  • pytest2.9.0 or newer

  • pytest-xdist 1.14 or above if you want the progress bar to work while running tests in parallel

我运行不生效的原因是:没有安装pytest-xdist,安装后,运行生效

3.不使用sugar用法:

py.test -p no:sugar

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们