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

作者:redrose2100 时间:2023-01-26 13:20:40 

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官方文档: https://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

来源:https://blog.csdn.net/redrose2100/article/details/128220829

标签:pytest,sugar,进度条
0
投稿

猜你喜欢

  • Python面向对象之类和实例用法分析

    2022-09-09 00:02:33
  • windows下python安装小白入门教程

    2022-01-05 08:45:44
  • python中pyqtgraph知识点总结

    2022-02-23 10:24:30
  • Python 开发工具通过 agent 代理使用的方法

    2022-10-19 05:15:31
  • 邮箱正则表达式实现代码(针对php)

    2024-05-03 15:48:55
  • cordova+vue+webapp使用html5获取地理位置的方法

    2024-04-27 16:00:05
  • 详解利用Python scipy.signal.filtfilt() 实现信号滤波

    2022-09-23 21:23:03
  • JS实现点击下拉菜单把选择的内容同步到input输入框内的实例

    2023-09-08 19:50:10
  • Python 列表反转显示的四种方法

    2021-07-21 02:41:31
  • django框架基于queryset和双下划线的跨表查询操作详解

    2022-02-22 04:30:32
  • Pycharm学习教程(5) Python快捷键相关设置

    2021-08-08 18:56:44
  • Go语言利用Unmarshal解析json字符串的实现

    2024-05-09 09:54:25
  • Pytorch Tensor基本数学运算详解

    2022-12-09 03:26:50
  • python石头剪刀布小游戏(三局两胜制)

    2021-07-24 00:06:30
  • Django前后端分离csrf token获取方式

    2021-03-15 04:23:43
  • python使用cPickle模块序列化实例

    2022-06-26 22:35:09
  • Go语言中slice作为参数传递时遇到的一些“坑”

    2023-08-05 02:05:12
  • Python虚拟环境virtualenv创建及使用过程图解

    2023-05-18 10:57:22
  • 关于Python 3中print函数的换行详解

    2021-04-09 09:57:48
  • tensorflow 使用flags定义命令行参数的方法

    2021-03-20 10:43:23
  • asp之家 网络编程 m.aspxhome.com