cmd2的外部测试插件。允许像从cmd2 pyscript一样外部调用命令。
项目描述
cmd2外部测试插件
目录
概述
此插件支持通过以与cmd2 pyscript内部相同的上下文公开访问cmd2命令来测试cmd2应用程序。这允许验证应用程序对pyscript的支持。
示例cmd2应用程序
以下简短示例显示了如何混合外部测试插件以创建测试您的cmd2应用程序的夹具。
定义您的cmd2应用程序
import cmd2
class ExampleApp(cmd2.Cmd):
"""An class to show how to use a plugin"""
def __init__(self, *args, **kwargs):
# gotta have this or neither the plugin or cmd2 will initialize
super().__init__(*args, **kwargs)
def do_something(self, arg):
self.last_result = 5
self.poutput('this is the something command')
定义测试夹具
在您的测试中,为您的cmd2应用程序定义一个夹具
import cmd2_ext_test
import pytest
class ExampleAppTester(cmd2_ext_test.ExternalTestMixin, ExampleApp):
def __init__(self, *args, **kwargs):
# gotta have this or neither the plugin or cmd2 will initialize
super().__init__(*args, **kwargs)
@pytest.fixture
def example_app():
app = ExampleAppTester()
app.fixture_setup()
yield app
app.fixture_teardown()
编写测试
现在编写您的测试,使用app_cmd
函数通过访问cmd2应用程序的命令来验证您的应用程序。这允许以用户键入的相同格式调用应用程序的命令。调用命令的结果与运行带有cmd2的pyscript命令的Python脚本返回的结果相匹配,该脚本提供stdout、stderr和命令的结果数据。
from cmd2 import CommandResult
def test_something(example_app):
# execute a command
out = example_app.app_cmd("something")
# validate the command output and result data
assert isinstance(out, CommandResult)
assert str(out.stdout).strip() == 'this is the something command'
assert out.data == 5
许可证
cmd2 使用非常自由的MIT许可证。我们邀请插件作者考虑这样做。