|
| 1 | +External Test Plugin |
| 2 | +==================== |
| 3 | + |
| 4 | +Overview |
| 5 | +~~~~~~~~ |
| 6 | + |
| 7 | +.. _cmd2_external_test_plugin: |
| 8 | + https://github.com/python-cmd2/cmd2-ext-test/ |
| 9 | + |
| 10 | +The cmd2_external_test_plugin_ supports testing of a cmd2 application by exposing access cmd2 commands with the same |
| 11 | +context as from within a cmd2 pyscript. This allows for verification of an application's support for pyscripts and |
| 12 | +enables the cmd2 application to be tested as part of a larger system integration test. |
| 13 | + |
| 14 | + |
| 15 | +Example cmd2 Application |
| 16 | +~~~~~~~~~~~~~~~~~~~~~~~~ |
| 17 | + |
| 18 | +The following short example shows how to mix in the external test plugin to create a fixture for testing |
| 19 | +your cmd2 application. |
| 20 | + |
| 21 | +Define your cmd2 application |
| 22 | + |
| 23 | +.. code-block:: python |
| 24 | +
|
| 25 | + import cmd2 |
| 26 | + class ExampleApp(cmd2.Cmd): |
| 27 | + """An class to show how to use a plugin""" |
| 28 | + def __init__(self, *args, **kwargs): |
| 29 | + # gotta have this or neither the plugin or cmd2 will initialize |
| 30 | + super().__init__(*args, **kwargs) |
| 31 | +
|
| 32 | + def do_something(self, arg): |
| 33 | + self.last_result = 5 |
| 34 | + self.poutput('this is the something command') |
| 35 | +
|
| 36 | +Defining the test fixture |
| 37 | +~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 38 | + |
| 39 | +In your test, define a fixture for your cmd2 application |
| 40 | + |
| 41 | +.. code-block:: python |
| 42 | +
|
| 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 | + app.fixture_setup() |
| 55 | + yield app |
| 56 | + app.fixture_teardown() |
| 57 | +
|
| 58 | +
|
| 59 | +Writing Tests |
| 60 | +~~~~~~~~~~~~~ |
| 61 | + |
| 62 | +Now write your tests that validate your application using the `app_cmd` function to access |
| 63 | +the cmd2 application's commands. This allows invocation of the application's commands in the |
| 64 | +same format as a user would type. The results from calling a command matches what is returned |
| 65 | +from running an python script with cmd2's pyscript command, which provides stdout, stderr, and |
| 66 | +the command's result data. |
| 67 | + |
| 68 | +.. code-block:: python |
| 69 | +
|
| 70 | + from cmd2 import CommandResult |
| 71 | +
|
| 72 | + def test_something(example_app): |
| 73 | + # execute a command |
| 74 | + out = example_app.app_cmd("something") |
| 75 | +
|
| 76 | + # validate the command output and result data |
| 77 | + assert isinstance(out, CommandResult) |
| 78 | + assert str(out.stdout).strip() == 'this is the something command' |
| 79 | + assert out.data == 5 |
| 80 | +
|
0 commit comments