|
| 1 | +# cmd2 External Test Plugin |
| 2 | + |
| 3 | +## Table of Contents |
| 4 | + |
| 5 | +- [Overview](#overview) |
| 6 | +- [Example cmd2 Application](#example-cmd2-application) |
| 7 | +- [Defining the test fixture](#defining-the-test-fixture) |
| 8 | +- [Writing Tests](#writing-tests) |
| 9 | +- [License](#license) |
| 10 | + |
| 11 | + |
| 12 | +## Overview |
| 13 | + |
| 14 | +This plugin supports testing of a cmd2 application by exposing access cmd2 commands with the same context |
| 15 | +as from within a cmd2 pyscript. This allows for verification of an application's support for pyscripts. |
| 16 | + |
| 17 | + |
| 18 | +## Example cmd2 Application |
| 19 | + |
| 20 | +The following short example shows how to mix in the external test plugin to create a fixture for testing |
| 21 | +your cmd2 application. |
| 22 | + |
| 23 | +Define your cmd2 application |
| 24 | + |
| 25 | +```python |
| 26 | +import cmd2 |
| 27 | +class ExampleApp(cmd2.Cmd): |
| 28 | + """An class to show how to use a plugin""" |
| 29 | + def __init__(self, *args, **kwargs): |
| 30 | + # gotta have this or neither the plugin or cmd2 will initialize |
| 31 | + super().__init__(*args, **kwargs) |
| 32 | + |
| 33 | + def do_something(self, arg): |
| 34 | + self.last_result = 5 |
| 35 | + self.poutput('this is the something command') |
| 36 | +``` |
| 37 | + |
| 38 | +## Defining the test fixture |
| 39 | + |
| 40 | +In your test, define a fixture for your cmd2 application |
| 41 | + |
| 42 | +```python |
| 43 | +import cmd2_ext_test |
| 44 | +import pytest |
| 45 | + |
| 46 | +class ExampleAppTester(cmd2_ext_test.ExternalTestMixin, ExampleApp): |
| 47 | + def __init__(self, *args, **kwargs): |
| 48 | + # gotta have this or neither the plugin or cmd2 will initialize |
| 49 | + super().__init__(*args, **kwargs) |
| 50 | + |
| 51 | +@pytest.fixture |
| 52 | +def example_app(): |
| 53 | + app = ExampleAppTester() |
| 54 | + return app |
| 55 | +``` |
| 56 | + |
| 57 | +## Writing Tests |
| 58 | + |
| 59 | +Now write your tests that validate your application using the `app_cmd` function to access |
| 60 | +the cmd2 application's commands. This allows invocation of the application's commands in the |
| 61 | +same format as a user would type. The results from calling a command matches what is returned |
| 62 | +from running an python script with cmd2's pyscript command, which provides stdout, stderr, and |
| 63 | +the command's result data. |
| 64 | + |
| 65 | +```python |
| 66 | +from cmd2 import CommandResult |
| 67 | + |
| 68 | +def test_something(example_app): |
| 69 | + # execute a command |
| 70 | + out = example_app.app_cmd("something") |
| 71 | + |
| 72 | + # validate the command output and result data |
| 73 | + assert isinstance(out, CommandResult) |
| 74 | + assert str(out.stdout).strip() == 'this is the something command' |
| 75 | + assert out.data == 5 |
| 76 | +``` |
| 77 | + |
| 78 | +## License |
| 79 | + |
| 80 | +cmd2 [uses the very liberal MIT license](https://github.com/python-cmd2/cmd2/blob/master/LICENSE). |
| 81 | +We invite plugin authors to consider doing the same. |
0 commit comments