Skip to content

Commit 96c8144

Browse files
authored
Merge pull request #947 from python-cmd2/pyscript_docs
Further additions to pyscript documenntation for Issue #644
2 parents a7511e1 + 033e203 commit 96c8144

File tree

5 files changed

+118
-4
lines changed

5 files changed

+118
-4
lines changed

docs/features/scripting.rst

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

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

100121
.. _conditional:
101122
https://github.com/python-cmd2/cmd2/blob/master/examples/scripts/conditional.py
102-
103-
See python_scripting_ example and associated conditional_ script for more
104-
information.

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

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ use_parentheses = true
1313

1414
[doc8]
1515
ignore-path=docs/_build,.git,.idea,.pytest_cache,.tox,.venv,.vscode,build,cmd2,examples,tests,cmd2.egg-info,dist,htmlcov,__pycache__,*.egg
16-
;max-line-length=99
16+
max-line-length=117
1717
verbose=0

0 commit comments

Comments
 (0)