[Python-Dev] order of unittest.TestCase execution?

Michael Hudson mwh@python.net
12 Nov 2001 09:58:31 -0500


Skip Montanaro <skip@pobox.com> writes:

>     Andreas> Have you been trying to put the database creation into the
>     Andreas> setUp() method that is executed before every testcase ?
> 
> No, I was just trying to mimic the structure of the test_xmlrpc.py module.
> It put all the cases into one class, so I did to.  I realized I wanted to
> run the creation test before the others and noticed that it was executing
> them in alphabetical order.  I was just wondering if I could rely on that or
> if that was just a quirk of either the current test_support or unittest
> implementations and couldn't be relied on.

Well, there's this:

    def getTestCaseNames(self, testCaseClass):
        """Return a sorted sequence of method names found within testCaseClass
        """
        testFnNames = filter(lambda n,p=self.testMethodPrefix: n[:len(p)] == p,
                             dir(testCaseClass))
        for baseclass in testCaseClass.__bases__:
            for testFnName in self.getTestCaseNames(baseclass):
                if testFnName not in testFnNames:  # handle overridden methods
                    testFnNames.append(testFnName)
        if self.sortTestMethodsUsing:
            testFnNames.sort(self.sortTestMethodsUsing)
        return testFnNames

in unittest.py & self.sortTestMethodsUsing seems to be cmp() by
default.  Don't know if this is documented or subject to change, though.

It seems you could define a subclass of TestLoader, override
getTestCaseNames and pass an instance as the testLoader arg to
unitest.main() if you want to be sure.

Cheers,
M.

-- 
  ARTHUR:  Why should he want to know where his towel is?
    FORD:  Everybody should know where his towel is.
  ARTHUR:  I think your head's come undone.
                    -- The Hitch-Hikers Guide to the Galaxy, Episode 7