[python-nl] testing

Johnny deBris johnny at johnnydebris.net
Fri Apr 23 12:37:36 CEST 2010

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. :)



More information about the Python-nl mailing list