From 136a19e5eb980eab5f37a61b1261c029440965b6 Mon Sep 17 00:00:00 2001 From: "Brian E. Granger" Date: Tue, 20 Aug 2013 21:43:40 -0700 Subject: [PATCH] Added base class for Notebook API tests. --- .../html/services/kernels/tests/test_api.py | 23 +++++++++++ IPython/html/tests/launchnotebook.py | 40 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 IPython/html/services/kernels/tests/test_api.py create mode 100644 IPython/html/tests/launchnotebook.py diff --git a/IPython/html/services/kernels/tests/test_api.py b/IPython/html/services/kernels/tests/test_api.py new file mode 100644 index 000000000..4dbf2f78d --- /dev/null +++ b/IPython/html/services/kernels/tests/test_api.py @@ -0,0 +1,23 @@ +"""Test the kernels service API.""" + + +import os +import sys +import json + +import requests + +from IPython.html.tests.launchnotebook import NotebookTestBase + + +class KernelAPITest(NotebookTestBase): + """Test the kernels web service API""" + + def base_url(self): + return super(KernelAPITest,self).base_url() + 'api/kernels' + + def test_no_kernels(self): + """Make sure there are no kernels running at the start""" + url = self.base_url() + r = requests.get(url) + assert r.json() == [] diff --git a/IPython/html/tests/launchnotebook.py b/IPython/html/tests/launchnotebook.py new file mode 100644 index 000000000..a83cbc38c --- /dev/null +++ b/IPython/html/tests/launchnotebook.py @@ -0,0 +1,40 @@ +"""Base class for notebook tests.""" + +import sys +import time +from subprocess import Popen, PIPE +from unittest import TestCase + +from IPython.utils.tempdir import TemporaryDirectory + + +class NotebookTestBase(TestCase): + """A base class for tests that need a running notebook. + + This creates an empty profile in a temp ipython_dir + and then starts the notebook server with a separate temp notebook_dir. + """ + + port = 12342 + + def setUp(self): + self.ipython_dir = TemporaryDirectory() + self.notebook_dir = TemporaryDirectory() + notebook_args = [ + sys.executable, '-c', + 'from IPython.html.notebookapp import launch_new_instance; launch_new_instance()', + '--port=%d' % self.port, + '--no-browser', + '--ipython-dir=%s' % self.ipython_dir.name, + '--notebook-dir=%s' % self.notebook_dir.name + ] + self.notebook = Popen(notebook_args, stdout=PIPE, stderr=PIPE) + time.sleep(3.0) + + def tearDown(self): + self.notebook.terminate() + self.ipython_dir.cleanup() + self.notebook_dir.cleanup() + + def base_url(self): + return 'http://localhost:%i/' % self.port