Skip to content

Commit 87aeaf4

Browse files
committed
Further additions to pyscript documenntation for Issue #644
1 parent a7511e1 commit 87aeaf4

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

docs/features/scripting.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,24 @@ where:
9494
* ``command`` and ``args`` are entered exactly like they would be entered by
9595
a user of your application.
9696

97+
Advanced Support
98+
~~~~~~~~~~~~~~~~
99+
100+
When implementing a command, setting ``self.last_result`` allows for application-specific
101+
data to be returned to a python script from the command. This can allow python scripts to
102+
make decisions based on the result of previous application commands.
103+
104+
The application command (default: ``app``) returns a ``cmd2.CommandResult`` for each command.
105+
The ``cmd2.CommandResult`` object provides the captured output to ``stdout`` and ``stderr``
106+
while a command is executing. Additionally, it provides the value that command stored in
107+
``self.last_result``.
108+
109+
Additionally, an external test Mixin plugin has been provided to allow for python based
110+
external testing of the application. For example, for system integration tests scenarios
111+
where the python application is a component of a larger suite of tools and components. This
112+
interface allows python based tests to call commands and validate results as part of a
113+
larger test suite. See :ref:`plugins/external_test:External Test Plugin`
114+
97115
.. _python_scripting:
98116
https://github.com/python-cmd2/cmd2/blob/master/examples/python_scripting.py
99117

docs/index.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ Examples
6767
examples/index
6868

6969

70+
Plugins
71+
=======
72+
73+
.. toctree::
74+
:maxdepth: 2
75+
76+
plugins/index
77+
78+
7079
API Reference
7180
=============
7281

docs/plugins/external_test.rst

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+

docs/plugins/index.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Plugins
2+
========
3+
4+
.. toctree::
5+
:maxdepth: 1
6+
7+
external_test

0 commit comments

Comments
 (0)