annotate wibble/tests.py @ 4:ad5a8748afcf

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