comparison test/test_demo.py @ 6324:3e33b22a3158

BAsic test of demo and server intialization. This needs more work. I exit the server by hacking serve_forever to raise a KeyboardInterrupt. Otherwise the test just hangs. I need a replacement that exits after one (or more configurable) connections and some way to background the server so the test can make the connections to the running server instance.
author John Rouillard <rouilj@ieee.org>
date Fri, 05 Feb 2021 00:09:08 -0500
parents
children 5a3a386aa8e7
comparison
equal deleted inserted replaced
6323:490d4350328f 6324:3e33b22a3158
1 import unittest
2 import os, sys, shutil
3
4 from roundup.demo import install_demo, run_demo
5
6 import roundup.scripts.roundup_server
7
8 # https://stackoverflow.com/questions/4219717/how-to-assert-output-with-nosetest-unittest-in-python
9 # lightly modified
10 from contextlib import contextmanager
11 _py3 = sys.version_info[0] > 2
12 if _py3:
13 from io import StringIO # py3
14 else:
15 from StringIO import StringIO # py2
16 @contextmanager
17 def captured_output():
18 new_out, new_err = StringIO(), StringIO()
19 old_out, old_err = sys.stdout, sys.stderr
20 try:
21 sys.stdout, sys.stderr = new_out, new_err
22 yield sys.stdout, sys.stderr
23 finally:
24 sys.stdout, sys.stderr = old_out, old_err
25
26 class TestDemo(unittest.TestCase):
27 def setUp(self):
28 self.home = os.path.abspath('_test_demo')
29
30 def tearDown(self):
31 try:
32 shutil.rmtree(self.home)
33 except FileNotFoundError:
34 pass
35
36 def testDemo(self):
37 with captured_output() as (out, err):
38 install_demo(self.home, 'anydbm', 'classic')
39 output = out.getvalue().strip()
40 print(output)
41
42 # dummy up the return of get_server so the serve_forever method
43 # raises keyboard interrupt exiting the server so the test exits.
44 gs = roundup.scripts.roundup_server.ServerConfig.get_server
45 def raise_KeyboardInterrupt():
46 raise KeyboardInterrupt
47
48 def test_get_server(self):
49 httpd = gs(self)
50 httpd.serve_forever = raise_KeyboardInterrupt
51 return httpd
52
53 roundup.scripts.roundup_server.ServerConfig.get_server = test_get_server
54
55 # Run under context manager to capture output of startup text.
56 with captured_output() as (out, err):
57 run_demo(self.home)
58 output = out.getvalue().strip()
59 print(output)
60 # if the server installed and started this will be the
61 # last line in the output.
62 self.assertIn("Keyboard Interrupt: exiting", output.split('\n'))
63
64

Roundup Issue Tracker: http://roundup-tracker.org/