[TIP] alphabetical order in testing

holger krekel holger at merlinux.eu
Wed Jan 20 15:51:47 PST 2010

On Wed, Jan 20, 2010 at 17:09 -0500, Mark Sienkiewicz wrote:
> holger krekel wrote:
>> On Wed, Jan 20, 2010 at 12:43 -0500, Mark Sienkiewicz wrote:
>>> Of course, it could be any other arbitrary order, but alphabetical is 
>>>  much easier to determine than "order that they appear in the file" 
>>> after  you import a module.
>> Huh?  Ordering functions by their file position is straight forward in 
>> Python.   
> In that case, there is some clever python feature that I don't know  
> about.  How does it work?  Is it easier than
>    import os
>    l = dir(os)

functions have a bytecode object which has a co_firstlineno line 
number - you can use that to sort. If you insist on oneliners: 

    sorted(vars(os).items(), key=lambda x: 
        getattr(getattr(x[1], 'func_code', None), 'co_firstlineno', 0))

In practise i'd rather write this as a five-liner. 

>> I agree that stable ordering is valuable for replicating test failures. 
>> Both file-position and alphabetical orderings are stable.    
> Yes.  There are good arguments in favor of each too.  There is also a  
> good argument for the ability to choose a random order, as long as you  
> can get exactly the same random order again when you want it.

yip, was discussed some time ago on this list IIRC. 


More information about the testing-in-python mailing list