【pytest单元测试框架】(4)click模块介绍

Click模块

  click模块是Flask的作者开发的一个第三方模块,用于快速创建命令行。它的作用与Python标准库的argparse相同,但是,使用起来更简单。

  click是一个第三方库,因此使用起来需要先行安装

安装click模块

使用pip命令即可完成模块的安装:

pip install click

基本使用

Click对argparse的主要改在在于易用性,使用click模块主要分为两个步骤:

  1. 使用@click.command() 装饰一个函数,使之成为命令行接口
  2. 使用@click.option() 等装饰函数,为其添加命令行选项

编写实例:

# -*- coding:utf-8 -*-
# filename:test_click.py
# author: click_team
# date: 2021/11/19

import click


@click.command()
@click.option("--a", default=1, help="number1")
@click.option("--b", prompt="input b", help="number2")
def add(a, b):
    a = int(a)
    b = int(b)
    click.echo("{0}和{1}相乘等于{2}".format(a, b, a*b))


if __name__ == "__main__":
    add()

--help执行结果:

D:\00test\base_practice\clickTest>python test_click.py --help
Usage: test_click.py [OPTIONS]

Options:
--a INTEGER number1
--b TEXT number2
--help Show this message and exit.

--default结果:

D:\00test\base_practice\clickTest>python test_click.py
input b: 9
1和9相乘等于9

加入参数--a和--b执行结果:

D:\00test\base_practice\clickTest>python test_click.py --a 8 --b 9
8和9相乘等于72

在上面的例子中,函数hello接受两个参数,分别是a和b,他们的取值从命令行中获取,这里我们使用了click模块中的command、option、echo,他们的作用如下:

  • command:使函数hello成为命令行接口
  • option:增加命令行选项
  • echo:输出结果,使用echo进行输出是为了更好的兼容性,因为python 2中的print是个语句,python 3中的print 是一个函数

其他参数

option最基本的用法就是通过指定命令行选项的名称,从命令行读取参数值,再将其传递给函数。option常用的参数含义:

  • default: 设置命令行参数的默认值
  • help:参数说明
  • type:参数类型,可以是string、int、float等
  • prompt:当在命令行中没有输入相应的参数时,会更具prompt提示用户输入
  • nargs:指定命令行参数接受的值的个数
  • required:是否为必填参数
# -*- coding:utf-8 -*-
# filename:test_click.py
# author: click_team
# date: 2021/11/19

import click


@click.command()
@click.option("--a", default=1, help="number1", type=int)
@click.option("--b", prompt="input b", help="number2", type=int, required=True)
@click.option("--c", prompt="input c", help="number3", type=int, required=True)
@click.option("--d", prompt="input d", help="str1", type=str)
@click.option("--e", nargs=2, type=float)
def mul(a, b, c, d, e):
    click.echo("乘数字:{0}和{1}相乘等于{2}".format(a, b, a*b))
    click.echo("乘字符串:{0}和{1}相乘等于{2}".format(c, d, c*d))
    click.echo("对一个参数输入两个值:{}".format(e))


if __name__ == "__main__":
    mul()

--help执行结果:

D:\00test\base_practice\clickTest>python test_click.py --help
Usage: test_click.py [OPTIONS]

Options:
  --a INTEGER   number1
  --b INTEGER   number2  [required]
  --c INTEGER   number3  [required]
  --d TEXT      str1
  --e FLOAT...
  --help        Show this message and exit.

加参数执行结果:

D:\00test\base_practice\clickTest>python test_click.py --a 8 --b 9 --c 5  --d 9 --e 10.00 11.00
乘数字:8和9相乘等于72
乘字符串:5和9相乘等于99999
对一个参数输入两个值:(10.0, 11.0)

注意:option中定义的参数名称,那么就需要用同名的变量进行接受。

扩展用法

场景一:我们限定用户输入的值,那么就需要使用Click模块中的Choice函数,Choice的参数是一个列表,该列表中列出所有可能的值。

import click


@click.command()
@click.option("--c", required=True, type=click.Choice(["START", "STOP"]), help="请输入START或STOP")
def get_command(c):
    click.echo("确认值为{}".format(c))


if __name__ == '__main__':
    get_command()

 --help执行结果:

D:\00test\base_practice\clickTest>python test_click2.py --help
Usage: test_click2.py [OPTIONS]

Options:
  --c [START|STOP]  请输入START或STOP  [required]
  --help            Show this message and exit.

--输入错误参数结果:

D:\00test\base_practice\clickTest>python test_click2.py --c ST
Usage: test_click2.py [OPTIONS]
Try 'test_click2.py --help' for help.

Error: Invalid value for '--c': 'ST' is not one of 'START', 'STOP'.

--输入正确值

D:\00test\base_practice\clickTest>python test_click2.py --c START
确认值为START

场景二:应用程序从命令行读取密码:

使用标准库中的argparse模块只能像输入普通参数一样输入密码。这种方式存在一定安全隐患,例如输入的密码会保存在history中,查看命令历史列表就能获取密码

在Click中,这个问题就能完美的解决,只需要是这prompt为True,那么我们就能交互式输入密码,设置hide_input为True,就能隐藏密码,设置confirmation_prompt为True,就可以进行密码的两次验证,使用起来非常便捷。

import click


@click.command()
@click.option("--p", prompt="your password", hide_input=True)
def test_passwd(p):
    click.echo("您的密码是{}".format(p))


if __name__ == '__main__':
    test_passwd()

执行结果:

D:\00test\base_practice\clickTest>python test_click2.py
your password:
您的密码是122212
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值