unittest: Calling tests in liner number order

Fuzzyman fuzzyman at gmail.com
Sat May 24 09:24:27 EDT 2008


On May 23, 10:36 am, Antoon Pardon <apar... at forel.vub.ac.be> wrote:
> Some time ago I asked whether is would be possible that unittest would
> perform the test in order of appearence in the file.
>
> The answers seemed to be negative. Since I really would like this
> behaviour I took the trouble of looking throught the source and
> I think I found a minor way to get this behaviour.
>
> Now my questions are:
>
> Are other people interrested in this behaviour?
> Does the following patch has a chance of being introduced in the
> standard python distribution?
>
> *** /usr/lib/python2.5/unittest.py      2008-04-17 16:26:37.000000000 +0200
> --- unittest.py 2008-05-23 11:19:57.000000000 +0200
> ***************
> *** 570,575 ****
> --- 570,577 ----
>           """
>           def isTestMethod(attrname, testCaseClass=testCaseClass, prefix=self.testMethodPrefix):
>               return attrname.startswith(prefix) and callable(getattr(testCaseClass, attrname))
> +         def getlinenr(name):
> +             return getattr(testCaseClass, name).im_func.func_code.co_firstlineno
>           testFnNames = filter(isTestMethod, dir(testCaseClass))
>           for baseclass in testCaseClass.__bases__:
>               for testFnName in self.getTestCaseNames(baseclass):
> ***************
> *** 577,582 ****
> --- 579,586 ----
>                       testFnNames.append(testFnName)
>           if self.sortTestMethodsUsing:
>               testFnNames.sort(self.sortTestMethodsUsing)
> +         else:
> +             testFnNames.sort(key=getlinenr)
>           return testFnNames


I second Roy's appreciation with you going to the trouble to post a
patch. There is another problem with your code though, it is dependent
on the CPython implementation. Currently unittest works *great* with
IronPython, which your code wouldn't.

Also, like others, I have had wonderful experiences of trying to track
down test failures that depend on the order that tests run in. Having
interdependencies between tests is a recipe for madness...

Michael Foord
http://www.ironpythoninaction.com/



More information about the Python-list mailing list