I have a long running simulation that I need to stop at a given time in the simulation and retrieve and observe some information and then allow the simulation to continue. I've recently started using a Test Driven Design method, unfortunately I have no idea how to unit test an application that drops into an interactive shell.
Here is the basic idea of what I am trying to do:
# peekaboo.py
from IPython import embed
from IPython.config.loader import Config
PAB_HEADER = 'Hello, my name is PAB. How may I help you?'
PAB_EXIT_MESSAGE = 'Goodbye, sir.'
PAB_PROMPT_IN_TEMPLATE = 'In [PAB \\#]: '
PAB_PROMPT_IN2_TEMPLATE = ' .\\D.: '
PAB_PROMPT_OUT_TEMPLATE = 'Out [PAB \\#]: '
def activate(**vars):
"""
Activate PAB 0.1 by starting an interactive shell and putting
the variables in the scope of the caller into the scope of this
method.
"""
# Add variables from caller to this scope
locals().update(vars)
cfg = None
try:
get_ipython
except NameError:
cfg = Config()
prompt_config = cfg.PromptManager
prompt_config.in_template = PAB_PROMPT_IN_TEMPLATE
prompt_config.in2_template = PAB_PROMPT_IN2_TEMPLATE
prompt_config.out_template = PAB_PROMPT_OUT_TEMPLATE
embed(config=cfg, header=PAB_HEADER, exit_msg=PAB_EXIT_MESSAGE)
And here is an example of how it the peek_a_boo module might be used:
# long_running_app.py
import peek_a_boo
import datetime
import random
start_dt = datetime.datetime(2013,1,1)
datetimes = [start_dt + datetime.timedelta(days=i) for i in range(10)]
dt_of_interest = datetime.datetime(2013, 1, 8)
def long_running_process(dts):
"""
simulate long running process
"""
some_data = {}
for dt in dts:
some_data[dt] = random.random()
if dt.date() == dt_of_interest.date():
peek_a_boo.activate(**locals())
return some_data
if __name__ == '__main__':
data = long_running_process(datetimes)
print data
My first inclination is to use mock and patch the embed method and verify that it has been called with the correct arguments, but I was wondering if anyone had other suggestions?
UPDATE:
So I am using nose for my unit test and I tried the following:
# test_peek_a_boo.py
import nose
import mock
class TestPeekABoo(object):
def setup(self):
pass
def teardown(self):
pass
@mock.patch('IPython.embed')
def test_activate(self, mock_embed):
"""
Test that the activate method calls IPython embed with the correct arguments
"""
import peek_a_boo
a = 'Hello'
b = 'World'
peek_a_boo.activate(**locals())
mock_embed.assert_called_once_with(header=peek_a_boo.PAB_HEADER, ...)
But when I run:
nosetests test_peek_a_boo.py
The process hangs. If I run:
nosetests test_peek_a_boo.py -s
I can see that I the process is dropping into the Interactive Shell.
UPDATE 2:
I was able to get the test above to run by importing peek_a_boo inside the test_method of the test class.
This tests that embed was actually call, but I'd like to be able to test that both a and b make it into the local scope of the activate method.
It seems like the solution that I came up with works, so I'll post it as a solution.
# test_peek_a_boo.py
import nose
import mock
class TestPeekABoo(object):
def setup(self):
pass
def teardown(self):
pass
@mock.patch('IPython.embed')
def test_activate(self, mock_embed):
"""
Test that the activate method calls IPython embed with the correct arguments
"""
import peek_a_boo
a = 'Hello'
b = 'World'
peek_a_boo.activate(**locals())
mock_embed.assert_called_once_with(header=peek_a_boo.PAB_HEADER, ...)