[Twisted-Python] trial and setUpClass

Jonathan Lange jml at mumak.net
Thu May 18 18:41:47 EDT 2006


On 5/18/06, Manlio Perillo <manlio_perillo at libero.it> wrote:
> Jonathan Lange ha scritto:
> > [...]
> >> Another question: there is a supported way to execute code before *all*
> >> TestCases run?
> >>
> >>
> >> Now I simply execute a synchronous function before defining test cases.
> >>
> >
> > No, there isn't.
> >
> > What are you trying to do?
> >
>
>
> Create some tables and functions on a PostgreSQL database.
>

In that case, here's what I'd do.  I'd create a couple of top-level
functions (maybe methods in a TestCase subclass -- who knows?), like
this:

def checkDB():
    # check the database is configured properly.
    # return False if it needs to be restored

def setUpDB():
    if not checkDB():
        # drop & create a bunch of tables

I'd then whack a call to setUpDB() in my setUp (_not_ setUpClass) for
every test case that needed to use the database.  That way, I'd know
that the data was always correct, and I wouldn't be reconfiguring the
database for every single test.

You could also refine the process so that setUpDB checks individual
tables rather than the whole of your schema.

Hope this helps,
jml




More information about the Twisted-Python mailing list