doctest stype命令行应用程序测试仪
clatter的Python项目详细描述
clatter是用于命令行应用程序的doctest风格的测试工具。它包装了其他测试套件,并允许它们在docstring中进行测试。
- 自由软件:麻省理工学院许可证
- 文档:https://clatter.readthedocs.io。
功能
- 将测试最佳实践带到您的命令行应用程序中
- 可扩展-使用任何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以查看并添加到我们的待办事项中。