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

Tom Cocagne tom.cocagne at gmail.com
Sat Oct 8 22:55:41 EDT 2011

   I recently ran into a similar problem while developing a native
python DBus implementation. Initially I implemented only the
client-side portion and pointed all my tests at the OS-provided
session bus. However, once I got around to implementing the
server-side portion of DBus, simply pointing all those "known-good"
client-side tests at the new bus implementation was a handy way to
test out the new code. To mechanism I used was to modify the
client-side testing module to use top-level module variables to tune
the connection setup code and then created "test_bus.py" with the
following content:

import sys

from twisted.trial import unittest

import test_client

# Force the test code to use the internal bus rather than the
# OS-native bus
test_client.USE_INTERNAL_BUS = True

# "Copy" the test cases into this module
m = sys.modules[ __name__ ]

for k,v in test_client.__dict__.iteritems():
    if isinstance(v, type) and issubclass(v, unittest.TestCase):
        setattr(m, type(k, (v,), dict()))

It's a bit of a hack, of course, but it's conceptually simple and
doesn't require much extra effort. Glyph's or Tim's suggestions may be
better for your project but I've been pretty happy with this mechanism
so far so I thought I'd toss it out there.



On Mon, Sep 26, 2011 at 5:24 PM, Glyph Lefkowitz
<glyph at twistedmatrix.com> wrote:
> On Sep 26, 2011, at 7:37 AM, Laurens Van Houtven wrote:
> Are there any better ways to pass test data to trial?
> Environment variables.
> This isn't a great option, but I'd say it's definitely a better one than
> modules which monkeypatch stuff.
> You can also have your test cases synthesize stuff out of data files or
> config files in your environment.
> I'm not really inclined to make this super easy, since tests should by
> definition not be terribly configurable: the more knobs like this you have
> to turn, the less meaningful your test suite is likely to be.
> So, today, I'd write something like this:
> TXIEVERY_USERNAME=foo TXIEVERY_PW=bar TXIEVERY_CERT=baz.pem trial txievery
> but if someone has a better thought, please chime in - this is by no means
> the last word on this.  And I'm facing a similar issue soon so I'd love to
> hear some diversity of opinion here.
> -glyph
> PS: love the module name.
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

More information about the Twisted-Python mailing list