test_client.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. """Tests for the KernelClient"""
  2. # Copyright (c) Jupyter Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. import os
  5. pjoin = os.path.join
  6. from unittest import TestCase
  7. from jupyter_client.kernelspec import KernelSpecManager, NoSuchKernel, NATIVE_KERNEL_NAME
  8. from ..manager import start_new_kernel
  9. from .utils import test_env
  10. import pytest
  11. from ipython_genutils.py3compat import string_types
  12. from IPython.utils.capture import capture_output
  13. TIMEOUT = 30
  14. class TestKernelClient(TestCase):
  15. def setUp(self):
  16. self.env_patch = test_env()
  17. self.env_patch.start()
  18. self.addCleanup(self.env_patch.stop)
  19. try:
  20. KernelSpecManager().get_kernel_spec(NATIVE_KERNEL_NAME)
  21. except NoSuchKernel:
  22. pytest.skip()
  23. self.km, self.kc = start_new_kernel(kernel_name=NATIVE_KERNEL_NAME)
  24. self.addCleanup(self.kc.stop_channels)
  25. self.addCleanup(self.km.shutdown_kernel)
  26. def test_execute_interactive(self):
  27. kc = self.kc
  28. with capture_output() as io:
  29. reply = kc.execute_interactive("print('hello')", timeout=TIMEOUT)
  30. assert 'hello' in io.stdout
  31. assert reply['content']['status'] == 'ok'
  32. def _check_reply(self, reply_type, reply):
  33. self.assertIsInstance(reply, dict)
  34. self.assertEqual(reply['header']['msg_type'], reply_type + '_reply')
  35. self.assertEqual(reply['parent_header']['msg_type'], reply_type + '_request')
  36. def test_history(self):
  37. kc = self.kc
  38. msg_id = kc.history(session=0)
  39. self.assertIsInstance(msg_id, string_types)
  40. reply = kc.history(session=0, reply=True, timeout=TIMEOUT)
  41. self._check_reply('history', reply)
  42. def test_inspect(self):
  43. kc = self.kc
  44. msg_id = kc.inspect('who cares')
  45. self.assertIsInstance(msg_id, string_types)
  46. reply = kc.inspect('code', reply=True, timeout=TIMEOUT)
  47. self._check_reply('inspect', reply)
  48. def test_complete(self):
  49. kc = self.kc
  50. msg_id = kc.complete('who cares')
  51. self.assertIsInstance(msg_id, string_types)
  52. reply = kc.complete('code', reply=True, timeout=TIMEOUT)
  53. self._check_reply('complete', reply)
  54. def test_kernel_info(self):
  55. kc = self.kc
  56. msg_id = kc.kernel_info()
  57. self.assertIsInstance(msg_id, string_types)
  58. reply = kc.kernel_info(reply=True, timeout=TIMEOUT)
  59. self._check_reply('kernel_info', reply)
  60. def test_comm_info(self):
  61. kc = self.kc
  62. msg_id = kc.comm_info()
  63. self.assertIsInstance(msg_id, string_types)
  64. reply = kc.comm_info(reply=True, timeout=TIMEOUT)
  65. self._check_reply('comm_info', reply)
  66. def test_shutdown(self):
  67. kc = self.kc
  68. msg_id = kc.shutdown()
  69. self.assertIsInstance(msg_id, string_types)
  70. reply = kc.shutdown(reply=True, timeout=TIMEOUT)
  71. self._check_reply('shutdown', reply)