annotate wibble/tests.py @ 8:685479d1f0a7

Tests available for both py2 and py3
author Ben Croston <ben@croston.org>
date Sun, 04 Sep 2011 23:37:41 +0100
parents 3a6f3193cc7d
children 23743b0f67ab
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
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
2 import unittest
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
3 import hashlib
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
4 from threading import Thread
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
5 import time
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
6 from wsgiref import simple_server
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
7 import platform
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
8
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
9 ##### server vvv #####
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
10 class api(object):
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
11 def mymethod(self):
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
12 return 'wibbler woz ere'
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
13
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
14 def echo(self, mystring):
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
15 return 'ECHO: ' + mystring
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
16
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
17 def raiseexception(self):
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
18 dividebyzeroerror = 1/0
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
19
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
20 def myauth(username, password, useragent=None):
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
21 return username == 'testuser' and \
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
22 hashlib.md5('s3cr3t').hexdigest() == password and \
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
23 useragent == 'wibble_unittest'
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
24
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
25 def make_server():
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
26 from server import JsonRpcApp
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
27 class myhandler(simple_server.WSGIRequestHandler):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
28 def log_request(self, *a, **b):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
29 pass # do not output log messages
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
30 application = JsonRpcApp(api(), auth=myauth)
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
31 return simple_server.make_server('localhost', 1337, application, handler_class=myhandler)
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
32 ##### server ^^^ #####
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
33
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
34 ##### client vvv #####
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
35 class NotAuthTest(unittest.TestCase):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
36 def runTest(self):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
37 from client.ServerProxy import ServerProxy, UnauthorisedException
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
38 self.client = ServerProxy('http://localhost:1337/',
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
39 username='testuser',
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
40 password='s3cr3t',
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
41 user_agent='InternetExploiter')
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
42 with self.assertRaises(UnauthorisedException):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
43 self.assertEqual(self.client.api.mymethod(),self.client.mymethod())
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
44
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
45 class WibbleTests(unittest.TestCase):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
46 def setUp(self):
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
47 from client.ServerProxy import ServerProxy
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
48 self.client = ServerProxy('http://localhost:1337/',
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
49 username='testuser',
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
50 password='s3cr3t',
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
51 user_agent='wibble_unittest')
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
52
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
53 class IgnoreClassNameTest(WibbleTests):
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
54 def runTest(self):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
55 self.assertEqual(self.client.api.mymethod(),self.client.mymethod())
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
56
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
57 class ExceptionTest(WibbleTests):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
58 def runTest(self):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
59 with self.assertRaises(Exception):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
60 self.client.raiseexception()
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
61
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
62 class BadRequestTest(WibbleTests):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
63 def runTest(self):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
64 from client.ServerProxy import BadRequestException
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
65 with self.assertRaises(BadRequestException):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
66 self.client.FunctionDoesNotExist()
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
67
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
68 class EchoTest(WibbleTests):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
69 def runTest(self):
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
70 if platform.python_version().startswith('3'):
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
71 POUND = '\u00A3'
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
72 else:
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
73 POUND = unicode('\u00A3')
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
74 self.assertEqual(self.client.echo(POUND), 'ECHO: ' + POUND)
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
75 self.assertEqual(self.client.echo('hello mum!'), 'ECHO: hello mum!')
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
76 ##### client ^^^ #####
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
77
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
78 finished = False
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
79 def suite():
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
80 if platform.python_version().startswith('2'):
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
81 # create server
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
82 def test_wrapper():
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
83 server = make_server()
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
84 while not finished:
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
85 server.handle_request()
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
86 thread = Thread(target=test_wrapper)
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
87 thread.start()
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
88 time.sleep(0.1) # wait for server thread to start
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
89
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
90 # tests are as client
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
91 suite = unittest.TestSuite()
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
92 suite.addTest(NotAuthTest())
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
93 suite.addTest(IgnoreClassNameTest())
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
94 suite.addTest(ExceptionTest())
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
95 suite.addTest(BadRequestTest())
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
96 suite.addTest(EchoTest())
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
97 return suite
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
98
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
99 if __name__ == '__main__':
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
100 import sys
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
101 if platform.python_version().startswith('2 ') and 'serve' in sys.argv:
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
102 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
103 server = make_server()
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
104 try:
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
105 server.serve_forever()
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
106 except KeyboardInterrupt:
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
107 sys.exit()
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
108
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
109 unittest.TextTestRunner(verbosity=2).run(suite())
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
110 finished = True
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
111
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
112 # make a dummy request to get server thread out of loop
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
113 try:
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
114 import urllib
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
115 urllib.urlopen('http://localhost:1337/')
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
116 except:
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
117 pass
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
118