diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-02-09 00:07:06 -0500 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-02-09 00:07:06 -0500 |
commit | 6eb8dde0d33df2d73f9c7ff75c6beddae5453ddd (patch) | |
tree | 44cbbfc579efa655df4167e88d568ec3d8c85af4 | |
parent | 303e9cb734d217fe4f142040518e3dd7a2a2b3ee (diff) | |
download | cmd2-git-6eb8dde0d33df2d73f9c7ff75c6beddae5453ddd.tar.gz |
First past at unit test for persistent history feature
Added pexpect to modules required for running unit tests.
This opens the door for carefully crafted complex unit tests to verify intricate behavior. Tests like this are somewhat painful to write and slow to execute. However, they can enable testing complicated interactive behavior that we otherwise probably would not be able to test.
-rw-r--r-- | docs/requirements.txt | 1 | ||||
-rwxr-xr-x | examples/persistent_history.py | 1 | ||||
-rwxr-xr-x | setup.py | 2 | ||||
-rw-r--r-- | tests/test_cmd2.py | 27 | ||||
-rw-r--r-- | tox.ini | 8 |
5 files changed, 38 insertions, 1 deletions
diff --git a/docs/requirements.txt b/docs/requirements.txt index fa4e3570..b50df7d1 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,3 +1,4 @@ pyparsing six pyperclip +contextlib2 diff --git a/examples/persistent_history.py b/examples/persistent_history.py index 21a2cbf3..e3f646bf 100755 --- a/examples/persistent_history.py +++ b/examples/persistent_history.py @@ -13,6 +13,7 @@ class Cmd2PersistentHistory(cmd2.Cmd): def __init__(self): """""" cmd2.Cmd.__init__(self, persistent_history_file='~/.persistent_history.cmd2', persistent_history_length=500) + self.prompt = 'ph> ' # ... your class code here ... @@ -77,7 +77,7 @@ if sys.version_info < (3, 0): INSTALL_REQUIRES += ['subprocess32'] # unittest.mock was added in Python 3.3. mock is a backport of unittest.mock to all versions of Python -TESTS_REQUIRE = ['mock', 'pytest'] +TESTS_REQUIRE = ['mock', 'pytest', 'pexpect'] DOCS_REQUIRE = ['sphinx', 'sphinx_rtd_theme', 'pyparsing', 'pyperclip', 'six'] setup( diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 5f56803d..454fdc1f 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -11,6 +11,7 @@ import io import tempfile import mock +import pexpect import pytest import six @@ -1525,3 +1526,29 @@ def test_poutput_none(base_app): out = base_app.stdout.buffer expected = '' assert out == expected + + +def test_persistent_history(request): + test_dir = os.path.dirname(request.module.__file__) + persistent_app = os.path.join(test_dir, '..', 'examples', 'persistent_history.py') + + # STart an instance of the persistent history example and send it a few commands + child = pexpect.spawn(persistent_app) + prompt = 'ph> ' + child.expect(prompt) + child.sendline('help') + child.expect(prompt) + child.sendline('help history') + child.expect(prompt) + child.sendline('quit') + child.close() + + # Start a 2nd instance of the persistent history example and send it an up arrow to verify persistent history + up_arrow = '\x1b[A' + child2 = pexpect.spawn(persistent_app) + child2.expect(prompt) + child2.send(up_arrow) + child2.expect('quit', timeout=5) + assert child2.after == b'quit' + + @@ -13,6 +13,7 @@ setenv = deps = codecov mock + pexpect pyparsing pyperclip pytest @@ -28,6 +29,7 @@ commands = deps = codecov mock + pexpect pyparsing pyperclip pyreadline @@ -43,6 +45,7 @@ commands = [testenv:py34] deps = mock + pexpect pyparsing pyperclip pytest @@ -53,6 +56,7 @@ commands = py.test -v -n2 [testenv:py35] deps = mock + pexpect pyparsing pyperclip pytest @@ -63,6 +67,7 @@ commands = py.test -v -n2 [testenv:py35-win] deps = mock + pexpect pyparsing pyperclip pyreadline @@ -75,6 +80,7 @@ commands = py.test -v -n2 deps = codecov mock + pexpect pyparsing pyperclip pytest @@ -88,6 +94,7 @@ commands = [testenv:py36-win] deps = mock + pexpect pyparsing pyperclip pyreadline @@ -99,6 +106,7 @@ commands = py.test -v -n2 [testenv:py37] deps = mock + pexpect pyparsing pyperclip pytest |