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