[Twisted-Python] Running the same trial test suite against a live server and a mock server

Tim Allen screwtape at froup.com
Mon Sep 26 19:29:30 EDT 2011


On Mon, Sep 26, 2011 at 01:37:16PM +0200, Laurens Van Houtven wrote:
> The obvious solution is to have a makeClient function defined in module
> scope for the given test module. I could then have a bin/liveTest.py that
> takes a username, password and cert, and monkeypatches that module. I'm
> assuming it can't be hard to tell trial to just take a test module and run
> it...
> 
> Are there any better ways to pass test data to trial?

If you have a particular set of tests you want to run against a mock
server and a real server, I'd be highly tempted to use inheritance:


    class ServerTestMixin:

	def _get_server_details(self):
	    raise NotImplementedError

	def test_foo(self):
	    server = self._get_server_details()
	    ...
	

    class MockServerTest(ServerTestMixin, unittest.TestCase):

	def setUp(self):
	    # start mock server
	
	def tearDown(self):
	    # shutdown mock server.

	def _get_server_details(self):
	    return MOCK_SERVER_DETAILS

    
    class LiveServerTest(ServerTestMixin, unittest.TestCase):

	def _get_server_details(self):
	    return LIVE_SERVER_DETAILS


The other trick I've used in a similar situation is to define
ServerTestMixin and MockServerTest in a module matching "test_*.py" so
that Trial will find it by default, and define LiveServerTest in
a module like "integration_tests.py" that won't be automatically run.

That way, you can test your code with trial and random fluctuations in
the live server won't cause spurious test failures, but you can pretty
easily run your tests against the live server whenever you want. If
you're using continuous integration, you could have a script that fails
if the unit tests break, but just warns if the integration tests break,
etc.



More information about the Twisted-Python mailing list