annotate wibble/tests.py @ 5:f5e3ba8cfcd0

Improved test framework
author Ben Croston <ben@croston.org>
date Wed, 31 Aug 2011 22:45:55 +0100
parents ad5a8748afcf
children 3a6f3193cc7d
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):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
11 def mymethod(self):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
12 return 'wibbler woz ere'
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
13
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
14 def myexception(self):
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
15 raise Exception('This is a test error')
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
16
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
17 def myauth(username, password, useragent=None):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
18 #raise Exception("This is a test error in auth")
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
19 return username == 'testuser' and \
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
20 hashlib.md5('s3cr3t').hexdigest() == password and \
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
21 useragent == 'wibble_unittest'
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
22
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
23 def make_server():
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
24 from server import JsonRpcApp
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
25 class myhandler(simple_server.WSGIRequestHandler):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
26 def log_request(self, *a, **b):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
27 pass # do not output log messages
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
28 application = JsonRpcApp(api(), auth=myauth)
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
29 return simple_server.make_server('localhost', 1337, application, handler_class=myhandler)
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
30 ##### server ^^^ #####
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
31
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
32 ##### client vvv #####
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
33 class WibbleTests(unittest.TestCase):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
34 def setUp(self):
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
35 from client.ServerProxy import ServerProxy
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
36 self.client = ServerProxy('http://localhost:1337/',
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
37 username='testuser',
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
38 password='s3cr3t',
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
39 user_agent='wibble_unittest')
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
40
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
41 class IgnoreClassNameTest(WibbleTests):
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
42 def runTest(self):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
43 self.assertEqual(self.client.api.mymethod(),self.client.mymethod())
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
44
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
45 #def client_tests():
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
46 # try:
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
47 # print(jsonrpc_client.wibble('this should fail'))
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
48 # except BadRequestException:
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
49 # pass # test passed
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
50 # else:
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
51 # raise Exception('Test failed (calling unknown method)')
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
52 #
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
53 # print 'All tests passed'
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
54
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
55 ##### client ^^^ #####
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
56
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
57 finished = False
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
58 def suite():
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
59 if platform.python_version().startswith('3'):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
60 # no tests for python 3 because server not ported yet
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
61 return unittest.TestSuite()
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
62 def test_wrapper():
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
63 server = make_server()
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
64 while not finished:
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
65 server.handle_request()
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
66 thread = Thread(target=test_wrapper)
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
67 thread.start()
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
68 time.sleep(0.1) # wait for server thread to start
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
69 suite = unittest.TestSuite()
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
70 suite.addTest(IgnoreClassNameTest())
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
71 return suite
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
72
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
73 if __name__ == '__main__':
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
74 finished = False
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
75 unittest.TextTestRunner(verbosity=2).run(suite())
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
76 finished = True
4
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 # make a dummy request to get server thread out of loop
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
79 try:
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
80 import urllib
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
81 urllib.urlopen('http://localhost:1337/')
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
82 except:
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
83 pass
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
84