Exhaustive Unit Testing

Benjamin musiccomposition at gmail.com
Fri Nov 28 01:29:52 CET 2008


On Nov 27, 5:47 pm, Terry Reedy <tjre... at udel.edu> wrote:
> bearophileH... at lycos.com wrote:
> > Emanuele D'Arrigo:
> >> I can fragment the code of the original method into one public method and a few private support methods.<
>
> > Python also support nested functions, that you can put into your
> > method. The problem is that often unit test functions aren't able to
> > test nested functions.
>
> > A question for other people: Can Python change a little to allow
> > nested functions to be tested? I think this may solve some of my
> > problems.
>
> The problem is that inner functions do not exist until the outer
> function is called and the inner def is executed.  And they cease to
> exist when the outer function returns unless returned or associated with
> a global name or collection.

Of course, you could resort to terrible evil like this:

import types

def f():
    def sub_function():
        return 4

nested_function = types.FunctionType(f.func_code.co_consts[1], {})
print nested_function() # Prints 4

But don't do that!

>
> A 'function' only needs to be nested if it is intended to be different
> (different default or closure) for each execution of its def.




More information about the Python-list mailing list