Skip to content

Commit 6eb8dde

Browse files
committed
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.
1 parent 303e9cb commit 6eb8dde

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

Diff for: docs/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pyparsing
22
six
33
pyperclip
4+
contextlib2

Diff for: examples/persistent_history.py

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Cmd2PersistentHistory(cmd2.Cmd):
1313
def __init__(self):
1414
""""""
1515
cmd2.Cmd.__init__(self, persistent_history_file='~/.persistent_history.cmd2', persistent_history_length=500)
16+
self.prompt = 'ph> '
1617

1718
# ... your class code here ...
1819

Diff for: setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
INSTALL_REQUIRES += ['subprocess32']
7878

7979
# unittest.mock was added in Python 3.3. mock is a backport of unittest.mock to all versions of Python
80-
TESTS_REQUIRE = ['mock', 'pytest']
80+
TESTS_REQUIRE = ['mock', 'pytest', 'pexpect']
8181
DOCS_REQUIRE = ['sphinx', 'sphinx_rtd_theme', 'pyparsing', 'pyperclip', 'six']
8282

8383
setup(

Diff for: tests/test_cmd2.py

+27
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import tempfile
1212

1313
import mock
14+
import pexpect
1415
import pytest
1516
import six
1617

@@ -1525,3 +1526,29 @@ def test_poutput_none(base_app):
15251526
out = base_app.stdout.buffer
15261527
expected = ''
15271528
assert out == expected
1529+
1530+
1531+
def test_persistent_history(request):
1532+
test_dir = os.path.dirname(request.module.__file__)
1533+
persistent_app = os.path.join(test_dir, '..', 'examples', 'persistent_history.py')
1534+
1535+
# STart an instance of the persistent history example and send it a few commands
1536+
child = pexpect.spawn(persistent_app)
1537+
prompt = 'ph> '
1538+
child.expect(prompt)
1539+
child.sendline('help')
1540+
child.expect(prompt)
1541+
child.sendline('help history')
1542+
child.expect(prompt)
1543+
child.sendline('quit')
1544+
child.close()
1545+
1546+
# Start a 2nd instance of the persistent history example and send it an up arrow to verify persistent history
1547+
up_arrow = '\x1b[A'
1548+
child2 = pexpect.spawn(persistent_app)
1549+
child2.expect(prompt)
1550+
child2.send(up_arrow)
1551+
child2.expect('quit', timeout=5)
1552+
assert child2.after == b'quit'
1553+
1554+

Diff for: tox.ini

+8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ setenv =
1313
deps =
1414
codecov
1515
mock
16+
pexpect
1617
pyparsing
1718
pyperclip
1819
pytest
@@ -28,6 +29,7 @@ commands =
2829
deps =
2930
codecov
3031
mock
32+
pexpect
3133
pyparsing
3234
pyperclip
3335
pyreadline
@@ -43,6 +45,7 @@ commands =
4345
[testenv:py34]
4446
deps =
4547
mock
48+
pexpect
4649
pyparsing
4750
pyperclip
4851
pytest
@@ -53,6 +56,7 @@ commands = py.test -v -n2
5356
[testenv:py35]
5457
deps =
5558
mock
59+
pexpect
5660
pyparsing
5761
pyperclip
5862
pytest
@@ -63,6 +67,7 @@ commands = py.test -v -n2
6367
[testenv:py35-win]
6468
deps =
6569
mock
70+
pexpect
6671
pyparsing
6772
pyperclip
6873
pyreadline
@@ -75,6 +80,7 @@ commands = py.test -v -n2
7580
deps =
7681
codecov
7782
mock
83+
pexpect
7884
pyparsing
7985
pyperclip
8086
pytest
@@ -88,6 +94,7 @@ commands =
8894
[testenv:py36-win]
8995
deps =
9096
mock
97+
pexpect
9198
pyparsing
9299
pyperclip
93100
pyreadline
@@ -99,6 +106,7 @@ commands = py.test -v -n2
99106
[testenv:py37]
100107
deps =
101108
mock
109+
pexpect
102110
pyparsing
103111
pyperclip
104112
pytest

0 commit comments

Comments
 (0)