"一种Doctest风格的命令行应用程序测试器"
项目描述
Bashdoctest是原始“clatter”仓库的分支,它是一个针对命令行应用程序的doctest风格的测试工具。它封装了其他测试套件,并允许它们在文档字符串中进行测试。
自由软件:MIT许可证
特性
将测试最佳实践带给您的命令行应用程序
可扩展 - 使用任何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的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 68eb5db3cf58a3a563d82f89dee5a86d5f064932bd0724e6447483e7a0526caf |
|
MD5 | 3b159a2f209690a1d2ed1844892e4546 |
|
BLAKE2b-256 | 4913bb90fd972abc89dfc4bee7cd5650ee75e7cb4a6a3683275e85bc628f2560 |
关闭
bashdoctest-0.1.2-py2.py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 5fe7730d10be4b3f276e9a2014d06b099ef94c3e6e0c78f69b2bb710be316592 |
|
MD5 | 4902a6d008ddec41bee182cae0092182 |
|
BLAKE2b-256 | f2b12a20d6a650f4f0a45ca1f506496a32570b609d20bd95d42196253fa391cd |