##// END OF EJS Templates
don't start new notebook server for each test *method*...
MinRK -
Show More
@@ -1,62 +1,69 b''
1 1 """Base class for notebook tests."""
2 2
3 3 import sys
4 4 import time
5 5 import requests
6 6 from subprocess import Popen, PIPE
7 7 from unittest import TestCase
8 8
9 9 from IPython.utils.tempdir import TemporaryDirectory
10 10
11 11
12 12 class NotebookTestBase(TestCase):
13 13 """A base class for tests that need a running notebook.
14 14
15 15 This creates an empty profile in a temp ipython_dir
16 16 and then starts the notebook server with a separate temp notebook_dir.
17 17 """
18 18
19 19 port = 12341
20 20
21 def wait_till_alive(self):
22 url = 'http://localhost:%i/' % self.port
21 @classmethod
22 def wait_until_alive(cls):
23 """Wait for the server to be alive"""
24 url = 'http://localhost:%i/api/notebooks' % cls.port
23 25 while True:
24 time.sleep(.1)
25 26 try:
26 r = requests.get(url + 'api/notebooks')
27 break
27 requests.get(url)
28 28 except requests.exceptions.ConnectionError:
29 pass
30
31 def wait_till_dead(self):
32 url = 'http://localhost:%i/' % self.port
29 time.sleep(.1)
30 else:
31 break
32
33 @classmethod
34 def wait_until_dead(cls):
35 """Wait for the server to stop getting requests after shutdown"""
36 url = 'http://localhost:%i/api/notebooks' % cls.port
33 37 while True:
34 time.sleep(.1)
35 38 try:
36 r = requests.get(url + 'api/notebooks')
37 continue
39 requests.get(url)
38 40 except requests.exceptions.ConnectionError:
39 41 break
40
41 def setUp(self):
42 self.ipython_dir = TemporaryDirectory()
43 self.notebook_dir = TemporaryDirectory()
42 else:
43 time.sleep(.1)
44
45 @classmethod
46 def setup_class(cls):
47 cls.ipython_dir = TemporaryDirectory()
48 cls.notebook_dir = TemporaryDirectory()
44 49 notebook_args = [
45 50 sys.executable, '-c',
46 51 'from IPython.html.notebookapp import launch_new_instance; launch_new_instance()',
47 '--port=%d' % self.port,
52 '--port=%d' % cls.port,
48 53 '--no-browser',
49 '--ipython-dir=%s' % self.ipython_dir.name,
50 '--notebook-dir=%s' % self.notebook_dir.name
51 ]
52 self.notebook = Popen(notebook_args, stdout=PIPE, stderr=PIPE)
53 self.wait_till_alive()
54
55 def tearDown(self):
56 self.notebook.terminate()
57 self.ipython_dir.cleanup()
58 self.notebook_dir.cleanup()
59 self.wait_till_dead()
60
61 def base_url(self):
62 return 'http://localhost:%i/' % self.port
54 '--ipython-dir=%s' % cls.ipython_dir.name,
55 '--notebook-dir=%s' % cls.notebook_dir.name
56 ]
57 cls.notebook = Popen(notebook_args, stdout=PIPE, stderr=PIPE)
58 cls.wait_until_alive()
59
60 @classmethod
61 def teardown_class(cls):
62 cls.notebook.terminate()
63 cls.ipython_dir.cleanup()
64 cls.notebook_dir.cleanup()
65 cls.wait_until_dead()
66
67 @classmethod
68 def base_url(cls):
69 return 'http://localhost:%i/' % cls.port
General Comments 0
You need to be logged in to leave comments. Login now