##// END OF EJS Templates
Adding calls to shell_channel.stop() to shutdown....
Adding calls to shell_channel.stop() to shutdown. But this hangs the test suite for IPython.frontend.html.notebook.

File last commit:

r8941:775571cd
r9118:31470a22
Show More
test_kernel.py
89 lines | 3.0 KiB | text/x-python | PythonLexer
epatters
TST: Add some unit tests for in-process kernel and kernel manager.
r8475 #-------------------------------------------------------------------------------
# Copyright (C) 2012 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-------------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from __future__ import print_function
# Standard library imports
epatters
BUG: raw_input logic incorrect for in-process terminal frontend.
r8482 from StringIO import StringIO
import sys
epatters
TST: Add some unit tests for in-process kernel and kernel manager.
r8475 import unittest
# Local imports
from IPython.inprocess.blockingkernelmanager import \
BlockingInProcessKernelManager
from IPython.inprocess.ipkernel import InProcessKernel
epatters
TST: Skip pylab test for in-process kernel when matplotlib is missing.
r8493 from IPython.testing.decorators import skipif_not_matplotlib
epatters
TST: Add some unit tests for in-process kernel and kernel manager.
r8475 from IPython.utils.io import capture_output
Pietro Berkes
BUG: In inprocess tests, use input/raw_input depending on Python version.
r8941 from IPython.utils import py3compat
epatters
TST: Add some unit tests for in-process kernel and kernel manager.
r8475
#-----------------------------------------------------------------------------
# Test case
#-----------------------------------------------------------------------------
class InProcessKernelTestCase(unittest.TestCase):
epatters
TST: Skip pylab test for in-process kernel when matplotlib is missing.
r8493 @skipif_not_matplotlib
epatters
TST: Add some unit tests for in-process kernel and kernel manager.
r8475 def test_pylab(self):
""" Does pylab work in the in-process kernel?
"""
km = BlockingInProcessKernelManager()
km.start_kernel()
km.shell_channel.execute('%pylab')
msg = get_stream_message(km)
self.assert_('Welcome to pylab' in msg['content']['data'])
epatters
BUG: raw_input logic incorrect for in-process terminal frontend.
r8482 def test_raw_input(self):
""" Does the in-process kernel handle raw_input correctly?
"""
km = BlockingInProcessKernelManager()
km.start_kernel()
io = StringIO('foobar\n')
sys_stdin = sys.stdin
sys.stdin = io
try:
Pietro Berkes
BUG: In inprocess tests, use input/raw_input depending on Python version.
r8941 if py3compat.PY3:
km.shell_channel.execute('x = input()')
else:
km.shell_channel.execute('x = raw_input()')
epatters
BUG: raw_input logic incorrect for in-process terminal frontend.
r8482 finally:
sys.stdin = sys_stdin
self.assertEqual(km.kernel.shell.user_ns.get('x'), 'foobar')
epatters
TST: Add some unit tests for in-process kernel and kernel manager.
r8475 def test_stdout(self):
""" Does the in-process kernel correctly capture IO?
"""
kernel = InProcessKernel()
with capture_output() as io:
kernel.shell.run_cell('print("foo")')
self.assertEqual(io.stdout, 'foo\n')
km = BlockingInProcessKernelManager(kernel=kernel)
kernel.frontends.append(km)
km.shell_channel.execute('print("bar")')
msg = get_stream_message(km)
self.assertEqual(msg['content']['data'], 'bar\n')
#-----------------------------------------------------------------------------
# Utility functions
#-----------------------------------------------------------------------------
def get_stream_message(kernel_manager, timeout=5):
""" Gets a single stream message synchronously from the sub channel.
"""
while True:
msg = kernel_manager.sub_channel.get_msg(timeout=timeout)
if msg['header']['msg_type'] == 'stream':
return msg
if __name__ == '__main__':
unittest.main()