doctest stype命令行应用程序测试仪

clatter的Python项目详细描述


https://img.shields.io/pypi/v/clatter.svghttps://travis-ci.org/delgadom/clatter.svg?branch=masterhttps://coveralls.io/repos/github/delgadom/clatter/badge.svg?branch=masterDocumentation StatusUpdateshttps://api.codacy.com/project/badge/Grade/2c2af36490c04543b925edafc0d66842https://img.shields.io/pypi/pyversions/clatter.svg

clatter是用于命令行应用程序的doctest风格的测试工具。它包装了其他测试套件,并允许它们在docstring中进行测试。

功能

  • 将测试最佳实践带到您的命令行应用程序中
  • 可扩展-使用任何cli测试套件对commandvalidator进行子类化都很简单
  • 轻松测试文档。这个自述文件是一个有效的医生!

用法

>>>fromclatterimportRunner>>>fromclatter.validatorsimportSubprocessValidator

使用特定于应用程序的测试引擎对命令行实用程序和应用程序进行白名单测试:

>>>test_str=r'''
...
... .. code-block:: bash
...
...     $ echo 'Pining for the fjords'
...     Pining for the fjords
... '''>>>>>>tester=Runner()>>>tester.call_engines['echo']=SubprocessValidator()>>>tester.teststring(test_str)

点击应用程序

集成您的命令行应用程序:

>>>importclick>>>@click.command()...@click.argument('name')...defhello(name):...click.echo('Hello %s!'%name)

现在可以在docstrings中进行测试:

>>>test_str='''
...
... .. code-block:: bash
...
...     $ hello Polly
...     Hello Polly!
...
...     $ hello Polly Parrot
...     Usage: hello [OPTIONS] NAME
...     <BLANKLINE>
...     Error: Got unexpected extra argument (Parrot)
...
...     $ hello 'Polly Parrot'
...     Hello Polly Parrot!
...
... '''

点击应用程序可以用ClickValidator引擎进行测试:

>>>fromclatter.validatorsimportClickValidator>>>tester=Runner()>>>tester.call_engines['hello']=ClickValidator(hello)>>>tester.teststring(test_str)

混合应用程序

通过添加多个引擎,您的应用程序可以与其他命令行实用程序结合:

>>>test_str=r'''
...
... .. code-block:: bash
...
...     $ hello Polly
...     Hello Polly!
...
...     $ echo 'Pining for the fjords'
...     Pining for the fjords
...
... Pipes/redirects don't work, so we can't redirect this value into a file.
... But we can write a file with python:
...
... .. code-block:: bash
...
...     $ python -c \
...     >     "with open('tmp.txt', 'w+') as f: f.write('Pushing up daisies')"
...
...     $ cat tmp.txt
...     Pushing up daisies
...
... '''>>>tester.call_engines['echo']=SubprocessValidator()>>>tester.call_engines['python']=SubprocessValidator()>>>tester.call_engines['cat']=SubprocessValidator()>>>tester.teststring(test_str)

抑制命令

使用SkipValidator

>>>test_str='''
... .. code-block:: bash
...
...     $ aws storage buckets list
...
... '''>>>fromclatter.validatorsimportSkipValidator>>>tester.call_engines['aws']=SkipValidator()>>>tester.teststring(test_str)

非法命令

使用未列入白名单的应用程序时会出现错误:

>>>test_str='''
...
... The following block of code should cause an error:
...
... .. code-block:: bash
...
...     $ rm tmp.txt
...
... '''>>>tester.teststring(test_str)# doctest +ELLIPSISTraceback(mostrecentcalllast):...ValueError:Command"rm"notallowed.Addcommandcallertocall_enginestowhitelist.

如果指定了+skip,则无法识别的命令不会引发错误

>>>test_str=r'''
...
... .. code-block:: bash
...
...     $ nmake all # doctest: +SKIP
...     $ echo 'I made it!'
...     I made it!
...
... '''>>>tester.teststring(test_str)

错误处理

与命令输出不匹配的行将引发错误

>>>test_str=r'''
... .. code-block:: bash
...
...     $ echo "There, it moved!"
...     "No it didn't!"
...
... '''>>>tester=Runner()>>>tester.call_engines['echo']=SubprocessValidator()>>>tester.teststring(test_str)# doctest: +ELLIPSIS +NORMALIZE_WHITESPACETraceback(mostrecentcalllast):...ValueError:Clattertestfailed.There,itmoved!!="No it didn't!"+There,itmoved!-"No it didn't!"

已知问题

我们的issues页面有问题。但我们想在这件事上非常坦率。

安全性

类似于doctest,从测试中执行任意命令是危险的,我们不会试图保护您。我们不会运行您没有白名单的命令,但我们无法防止恶意案例。不要运行任何你不明白的东西,并使用自己的风险。

句法完整性

clatter不是一个语法上完全的bash仿真器,也无意如此。

命令的所有参数都作为参数传递给第一个命令。因此,循环、管道、重定向以及其他控制流和IO命令将无法按预期工作。

>>>test_str='''
...    $ echo hello > test.txt
...    $ cat test.txt
...    hello
...
... '''>>>tester.teststring(test_str)# doctest: +ELLIPSIS +NORMALIZE_WHITESPACETraceback(mostrecentcalllast):...ValueError:Clattertestfailed.hello>test.txt!=+hello>test.txt-

安装

pip install clatter

要求

  • pytest

待办事项

请参见issues以查看并添加到我们的待办事项中。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
JavaSpring重定向请求处理程序   SwingJava:拆分字符串并将其放入文本区域的   Java:标记“”上出现语法错误,此标记后面应为表达式   web服务Java RestService从日志文件写入和读取数据   java如何将ArrayList<String>转换为char数组,然后向后打印每个单词?   java SimpleDataFormat解析返回年终日期   加密Java aes解密bytebuffer,包括填充为空字节   java有没有办法从特定的if语句调用变量?   java从更新返回到渲染   spring GRPC Java登录测试   java为什么下面的代码不工作(StringBuffer.toString!=null)   java是一种可行的模式吗?   使用Spring集成测试的JavaOSGi片段   java jCommander为未知和未使用的值引发异常?   在imageView的editText中输入的java图像URL