annotate wibble/tests.py @ 14:45c1d78559e2

Added more tests
author Ben Croston <ben@croston.org>
date Mon, 05 Sep 2011 13:08:07 +0100
parents 4ac14cfadc15
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
1 #!/usr/bin/env python
10
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
2
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
3 # Copyright (c) 2011 Ben Croston
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
4 #
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
5 # Permission is hereby granted, free of charge, to any person obtaining a copy of
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
6 # this software and associated documentation files (the "Software"), to deal in
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
7 # the Software without restriction, including without limitation the rights to
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
8 # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
9 # of the Software, and to permit persons to whom the Software is furnished to do
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
10 # so, subject to the following conditions:
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
11 #
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
12 # The above copyright notice and this permission notice shall be included in all
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
13 # copies or substantial portions of the Software.
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
14 #
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
21 # SOFTWARE.
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
22
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
23 import unittest
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
24 import hashlib
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
25 from threading import Thread
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
26 import time
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
27 from wsgiref import simple_server
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
28 import platform
14
45c1d78559e2 Added more tests
Ben Croston <ben@croston.org>
parents: 12
diff changeset
29 import urllib
45c1d78559e2 Added more tests
Ben Croston <ben@croston.org>
parents: 12
diff changeset
30
45c1d78559e2 Added more tests
Ben Croston <ben@croston.org>
parents: 12
diff changeset
31 try:
45c1d78559e2 Added more tests
Ben Croston <ben@croston.org>
parents: 12
diff changeset
32 urllib.urlopen('http://www.wyre-it.co.uk/')
45c1d78559e2 Added more tests
Ben Croston <ben@croston.org>
parents: 12
diff changeset
33 NO_INTERNET = False
45c1d78559e2 Added more tests
Ben Croston <ben@croston.org>
parents: 12
diff changeset
34 except IOError:
45c1d78559e2 Added more tests
Ben Croston <ben@croston.org>
parents: 12
diff changeset
35 NO_INTERNET = True
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
36
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
37 ##### server vvv #####
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
38 class api(object):
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
39 def mymethod(self):
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
40 return 'wibbler woz ere'
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
41
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
42 def echo(self, mystring):
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
43 return 'ECHO: ' + mystring
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
44
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
45 def raiseexception(self):
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
46 dividebyzeroerror = 1/0
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
47
11
dbd55e6af4f6 Added test
Ben Croston <ben@croston.org>
parents: 10
diff changeset
48 def returnnothing(self):
dbd55e6af4f6 Added test
Ben Croston <ben@croston.org>
parents: 10
diff changeset
49 pass
dbd55e6af4f6 Added test
Ben Croston <ben@croston.org>
parents: 10
diff changeset
50
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
51 def myauth(username, password, useragent=None):
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
52 return username == 'testuser' and \
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
53 hashlib.md5('s3cr3t').hexdigest() == password and \
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
54 useragent == 'wibble_unittest'
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
55
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
56 def make_server():
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
57 from server import JsonRpcApp
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
58 class myhandler(simple_server.WSGIRequestHandler):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
59 def log_request(self, *a, **b):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
60 pass # do not output log messages
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
61 application = JsonRpcApp(api(), auth=myauth)
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
62 return simple_server.make_server('localhost', 1337, application, handler_class=myhandler)
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
63 ##### server ^^^ #####
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
64
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
65 ##### client vvv #####
12
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
66 class AuthTest(unittest.TestCase):
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
67 def runTest(self):
9
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 8
diff changeset
68 from client import ServerProxy, UnauthorisedException
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
69 self.client = ServerProxy('http://localhost:1337/',
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
70 username='testuser',
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
71 password='s3cr3t',
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
72 user_agent='InternetExploiter')
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
73 with self.assertRaises(UnauthorisedException):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
74 self.assertEqual(self.client.api.mymethod(),self.client.mymethod())
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
75
12
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
76 self.client = ServerProxy('http://localhost:1337/',
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
77 username='testuser',
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
78 password='wrongpassword',
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
79 user_agent='wibble_unittest')
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
80 with self.assertRaises(UnauthorisedException):
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
81 self.assertEqual(self.client.api.mymethod(),self.client.mymethod())
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
82
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
83 self.client = ServerProxy('http://localhost:1337/',
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
84 username='wronguser',
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
85 password='s3cr3t',
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
86 user_agent='wibble_unittest')
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
87 with self.assertRaises(UnauthorisedException):
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
88 self.assertEqual(self.client.api.mymethod(),self.client.mymethod())
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
89
14
45c1d78559e2 Added more tests
Ben Croston <ben@croston.org>
parents: 12
diff changeset
90
45c1d78559e2 Added more tests
Ben Croston <ben@croston.org>
parents: 12
diff changeset
91 @unittest.skipIf(NO_INTERNET,'www.wyre-it.co.uk:80 not contactable')
45c1d78559e2 Added more tests
Ben Croston <ben@croston.org>
parents: 12
diff changeset
92 class NotFoundTest(unittest.TestCase):
45c1d78559e2 Added more tests
Ben Croston <ben@croston.org>
parents: 12
diff changeset
93 def runTest(self):
45c1d78559e2 Added more tests
Ben Croston <ben@croston.org>
parents: 12
diff changeset
94 from client import ServerProxy, NotFoundException
45c1d78559e2 Added more tests
Ben Croston <ben@croston.org>
parents: 12
diff changeset
95 self.client = ServerProxy('http://www.wyre-it.co.uk/notfound.txt')
45c1d78559e2 Added more tests
Ben Croston <ben@croston.org>
parents: 12
diff changeset
96 with self.assertRaises(NotFoundException):
45c1d78559e2 Added more tests
Ben Croston <ben@croston.org>
parents: 12
diff changeset
97 self.assertEqual(self.client.api.mymethod(),self.client.mymethod())
45c1d78559e2 Added more tests
Ben Croston <ben@croston.org>
parents: 12
diff changeset
98
45c1d78559e2 Added more tests
Ben Croston <ben@croston.org>
parents: 12
diff changeset
99 class NetworkSocketTest(unittest.TestCase):
45c1d78559e2 Added more tests
Ben Croston <ben@croston.org>
parents: 12
diff changeset
100 def runTest(self):
45c1d78559e2 Added more tests
Ben Croston <ben@croston.org>
parents: 12
diff changeset
101 from client import ServerProxy, NetworkSocketException
45c1d78559e2 Added more tests
Ben Croston <ben@croston.org>
parents: 12
diff changeset
102 self.client = ServerProxy('http://localhost:666/')
45c1d78559e2 Added more tests
Ben Croston <ben@croston.org>
parents: 12
diff changeset
103 with self.assertRaises(NetworkSocketException):
45c1d78559e2 Added more tests
Ben Croston <ben@croston.org>
parents: 12
diff changeset
104 self.assertEqual(self.client.api.mymethod(),self.client.mymethod())
45c1d78559e2 Added more tests
Ben Croston <ben@croston.org>
parents: 12
diff changeset
105
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
106 class WibbleTests(unittest.TestCase):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
107 def setUp(self):
9
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 8
diff changeset
108 from client import ServerProxy
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
109 self.client = ServerProxy('http://localhost:1337/',
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
110 username='testuser',
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
111 password='s3cr3t',
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
112 user_agent='wibble_unittest')
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
113
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
114 class IgnoreClassNameTest(WibbleTests):
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
115 def runTest(self):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
116 self.assertEqual(self.client.api.mymethod(),self.client.mymethod())
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
117
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
118 class ExceptionTest(WibbleTests):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
119 def runTest(self):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
120 with self.assertRaises(Exception):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
121 self.client.raiseexception()
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
122
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
123 class BadRequestTest(WibbleTests):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
124 def runTest(self):
9
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 8
diff changeset
125 from client import BadRequestException
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
126 with self.assertRaises(BadRequestException):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
127 self.client.FunctionDoesNotExist()
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
128
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
129 class EchoTest(WibbleTests):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
130 def runTest(self):
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
131 if platform.python_version().startswith('3'):
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
132 POUND = '\u00A3'
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
133 else:
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
134 POUND = unicode('\u00A3')
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
135 self.assertEqual(self.client.echo(POUND), 'ECHO: ' + POUND)
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
136 self.assertEqual(self.client.echo('hello mum!'), 'ECHO: hello mum!')
11
dbd55e6af4f6 Added test
Ben Croston <ben@croston.org>
parents: 10
diff changeset
137
dbd55e6af4f6 Added test
Ben Croston <ben@croston.org>
parents: 10
diff changeset
138 class ReturnNothing(WibbleTests):
dbd55e6af4f6 Added test
Ben Croston <ben@croston.org>
parents: 10
diff changeset
139 def runTest(self):
dbd55e6af4f6 Added test
Ben Croston <ben@croston.org>
parents: 10
diff changeset
140 self.assertEqual(self.client.returnnothing(), None)
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
141 ##### client ^^^ #####
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
142
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
143 finished = False
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
144 def suite():
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
145 if platform.python_version().startswith('2'):
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
146 # create server
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
147 def test_wrapper():
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
148 server = make_server()
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
149 while not finished:
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
150 server.handle_request()
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
151 thread = Thread(target=test_wrapper)
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
152 thread.start()
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
153 time.sleep(0.1) # wait for server thread to start
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
154
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
155 # tests are as client
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
156 suite = unittest.TestSuite()
12
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
157 suite.addTest(AuthTest())
14
45c1d78559e2 Added more tests
Ben Croston <ben@croston.org>
parents: 12
diff changeset
158 suite.addTest(NotFoundTest())
45c1d78559e2 Added more tests
Ben Croston <ben@croston.org>
parents: 12
diff changeset
159 suite.addTest(NetworkSocketTest())
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
160 suite.addTest(IgnoreClassNameTest())
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
161 suite.addTest(ExceptionTest())
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
162 suite.addTest(BadRequestTest())
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
163 suite.addTest(EchoTest())
11
dbd55e6af4f6 Added test
Ben Croston <ben@croston.org>
parents: 10
diff changeset
164 suite.addTest(ReturnNothing())
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
165 return suite
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
166
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
167 if __name__ == '__main__':
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
168 import sys
9
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 8
diff changeset
169 if platform.python_version().startswith('2') and 'serve' in sys.argv:
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
170 print 'Listening on port 1337 (Ctrl-C qo quit)...'
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
171 server = make_server()
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
172 try:
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
173 server.serve_forever()
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
174 except KeyboardInterrupt:
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
175 sys.exit()
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
176
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
177 unittest.TextTestRunner(verbosity=2).run(suite())
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
178 finished = True
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
179
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
180 # make a dummy request to get server thread out of loop
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
181 try:
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
182 import urllib
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
183 urllib.urlopen('http://localhost:1337/')
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
184 except:
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
185 pass
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
186