8

Is there any way to write unittests or doctests for innerfunc?

def outerfunc():
    def innerfunc():
        do_something()
    return innerfunc()
Gattster
  • 4,613
  • 5
  • 27
  • 39
  • 1
    You normally don't do this: the API is the enclosing function, and inner functions are analogous to named blocks of code in the function. You don't test them directly, just as you don't test a single loop inside a function directly; you test the inputs to the function that exercise the code block. – Glenn Maynard Jan 26 '10 at 08:27

2 Answers2

8

Only if you provide a way to extract the inner function object itself, e.g.

def outerfunc(calltheinner=True):
    def innerfunc():
        do_something()
    if calltheinner:
        return innerfunc()
    else:
        return innerfunc

If your outer function insists on hiding the inner one entirely inside itself (never letting it percolate outside when properly cajoled to do so), your unit-tests are powerless to defeat this strong bid for extreme and total privacy;-).

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
4

This is actually an old open Python issue:

There's a candidate patch (from 2007) that makes doctest find nested functions, but someone probably needs to push this.

Pi Delport
  • 10,356
  • 3
  • 36
  • 50