Used for creating blackbox tests of applications that depend on external urls. Enables creation of tests using a mocking style.
A test example
-
class FunctionalTest(unittest.TestCase):
-
def setUp(self):
-
self.server = StubServer(8998)
-
self.server.run()
-
-
def tearDown(self):
-
self.server.stop() # implicitly verifies that the expectations were satisfied
-
-
def test_get_with_file_call(self):
-
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>")
-
address = Address.objects.get(city="Calgary")
-
self.assertEquals("Early Drive", address.street)
-
-
def test_put_with_capture(self):
-
capture = {}
-
self.server.expect(method="PUT", url="/address/\d+$", data_capture=capture).and_return(reply_code=201)
-
trigger_application() # in this case, a batch process triggered by calling its run.sh file
-
try:
-
captured = capture["body"]
-
self.assertEquals("world", captured)
-
finally:
-
f.close()
-
