跳转到主要内容

"一种Doctest风格的命令行应用程序测试器"

项目描述

Bashdoctest是原始“clatter”仓库的分支,它是一个针对命令行应用程序的doctest风格的测试工具。它封装了其他测试套件,并允许它们在文档字符串中进行测试。

特性

  • 将测试最佳实践带给您的命令行应用程序

  • 可扩展 - 使用任何cli测试套件简单地子类化CommandValidator

  • 轻松测试您的文档。本README是一个有效的doctest!

用法

>>> from bashdoctest import Runner
>>> from bashdoctest.validators import SubprocessValidator

通过使用特定应用程序的测试引擎将命令行实用程序和应用程序列入白名单来测试它们

>>> 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)
# echo 'Pining for the fjords'

Click应用程序

集成您的命令行应用程序

>>> import click
>>> @click.command()
... @click.argument('name')
... def hello(name):
...     click.echo('Hello %s!' % name)

现在可以在文档字符串中进行测试

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

可以使用ClickValidator引擎测试Click应用程序

>>> from bashdoctest.validators import ClickValidator
>>> tester = Runner()
>>> tester.call_engines['hello'] = ClickValidator(hello)

>>> tester.teststring(test_str)
# hello Polly
# hello Polly Parrot
# hello 'Polly Parrot'

混合应用程序

您可以通过添加多个引擎将应用程序与其他命令行实用程序组合

>>> 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)
# hello Polly
# echo 'Pining for the fjords'
# python -c "with open('tmp.txt', 'w+') as f: f.write('Pushing up daisies')"
# cat tmp.txt

抑制命令

可以使用SkipValidator完全跳过命令

>>> test_str = '''
... .. code-block:: bash
...
...     $ aws storage buckets list --password $MY_PASSWORD
...
... '''

>>> from bashdoctest.validators import SkipValidator
>>> tester.call_engines['aws'] = SkipValidator()

>>> tester.teststring(test_str)
# aws storage ...

非法命令

当使用您未列入白名单的应用程序时,将引发错误

>>> test_str = '''
...
... The following block of code should cause an error:
...
... .. code-block:: bash
...
...     $ rm tmp.txt
...
... '''

>>> tester.teststring(test_str) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
ValueError: Command "rm" not allowed. Add command caller to call_engines to whitelist.

如果指定了+SKIP,则未知命令不会引发错误

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

错误处理

未能匹配命令输出的行将引发错误

>>> 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_WHITESPACE
Traceback (most recent call last):
...
ValueError: Differences (ndiff with -expected +actual):
    - "No it didn't!"
    + There, it moved!

已知问题

我们在我们问题页面上遇到一些问题。但我们希望对这些问题的态度非常坦诚。

安全性

doctest类似,在测试中执行任意命令是非常危险的,我们并没有尝试保护您。我们不会运行您未列入白名单的命令,但我们无法阻止恶意情况的发生。不要运行您不理解的内容,并自行承担风险。

语法完整性

Bashhdoctest不是一个语法完整的bash模拟器,也没有成为这样的打算。

所有命令的参数都作为第一个命令的参数传递。因此,循环、管道、重定向和其他控制流和I/O命令将不会按预期工作。

>>> test_str = '''
...    $ echo hello > test.txt
...    $ cat test.txt
...    hello
...
... '''
>>> tester.teststring(test_str) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
Traceback (most recent call last):
...
ValueError: Differences (ndiff with -expected +actual):
    + hello > test.txt
<BLANKLINE>

安装

pip install bashhdoctest

要求

  • pytest

待办事项

查看问题以查看和添加我们的待办事项。

项目详情


下载文件

下载您平台的文件。如果您不确定要选择哪个,请了解更多关于安装包的信息。

源分布

bashdoctest-0.1.2.tar.gz (20.2 kB 查看哈希值)

上传时间

构建分布

bashdoctest-0.1.2-py2.py3-none-any.whl (7.3 kB 查看哈希值)

上传时间 Python 2 Python 3

由以下组织支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面