Skip to content

Commit e34547c

Browse files
committed
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.
1 parent 519d3df commit e34547c

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

test/test_demo.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+

0 commit comments

Comments
 (0)