[python-nl] testing

C.T. Matsumoto c.t.matsumoto at gmail.com
Fri Apr 23 13:12:12 CEST 2010


Cheers Guido,

Yeah I'm with you that you should not be testing *how* the function is 
going about doing things... but lets say your making a function to 
format string output. On the one hand your function contains everything 
nicely enough, without bifurcating the code into lots of small pieces. 
After all doing ' '.join(rjust(9)).bla.bla.bla is simple line of code, 
but you still want to make sure you have the right format in the end. To 
take a single line of code that uses a lot of the built-in string 
functions and wrap it in another function seemed more trouble than it 
was worth.

At least for this example, it seems funny that some of the convenience 
functions on a type, when strung together can do some pretty weird 
things that need checking, but nevertheless are nicely placed in the 
function your are using them in. A better example is re.

T

Johnny deBris wrote:
> C.T. Matsumoto wrote:
>> Does anyone know a good way to test function objects? It's easy enough 
>> to test class attributes, but for some reason I'm finding it difficult 
>> to test if a function has the right 'attributes'.
>>
> Do you mean local variables? In that case, I would suggest not to test 
> those - the idea is that you test whether your function does the right 
> thing, not _how_ it does that... You test the results of certain 
> arguments (both expected and unexpected), and whether the function 
> returns or raises the right thing when those arguments are provided, in 
> the most optimal situation even before you write code, what local 
> variables the function uses internally does not matter, as long as it 
> does the right thing.
> 
> To answer your question though: theoretically you could ask a function 
> for its constant values (foo.func_code.co_locals) and for the name of 
> its local variables (foo.func_code.co_varnames), then remove the first 
> item from the const list (not sure what that is, seems to always be 
> None) and remove the first items that are variables from the local 
> variable names list (see foo.func_code.co_argcount), or something like 
> that (I may be missing some things here, but I guess you'll have plenty 
> to play with from here. :)
> 
> Cheers,
> 
> Guido
> 
> _______________________________________________
> Python-nl mailing list
> Python-nl at python.org
> http://mail.python.org/mailman/listinfo/python-nl
> 


-- 
C.T. Matsumoto
Claes de Vrieselaan 60a III
3021 JR Rotterdam
The Netherlands

tel.: +31 (0)6 41 45 08 54


More information about the Python-nl mailing list