ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-11-19 18:32:59  阅读:138  来源: 互联网

标签:__ help -- py 单元测试 pytest test 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

标签:__,help,--,py,单元测试,pytest,test,click
来源: https://www.cnblogs.com/yinzuopu/p/15578660.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有