Categories

stubserver

Used for creating blackbox tests of applications that depend on external urls. Enables creation of tests using a mocking style.

A test example

  1. class FunctionalTest(unittest.TestCase):
  2.     def setUp(self):
  3.         self.server = StubServer(8998)
  4.         self.server.run()
  5.        
  6.     def tearDown(self):
  7.         self.server.stop() # implicitly verifies that the expectations were satisfied
  8.        
  9.     def test_get_with_file_call(self):
  10.         self.server.expect(method="GET", url="/address/\w+$").and_return(mime_type="text/xml", content="<address><number>12</number><street>Early Drive</street><city>Calgary</city></address>")
  11.         address = Address.objects.get(city="Calgary")
  12.         self.assertEquals("Early Drive", address.street)
  13.  
  14.     def test_put_with_capture(self):
  15.         capture = {}
  16.         self.server.expect(method="PUT", url="/address/\d+$", data_capture=capture).and_return(reply_code=201)
  17.         trigger_application() # in this case, a batch process triggered by calling its run.sh file
  18.         try:
  19.             captured = capture["body"]
  20.             self.assertEquals("world", captured)
  21.         finally:
  22.             f.close()
  23.