Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I unit test an inner function in python?

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

def outerfunc():
    def innerfunc():
        do_something()
    return innerfunc()
like image 911
Gattster Avatar asked Jan 26 '10 00:01

Gattster


2 Answers

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

like image 128
Alex Martelli Avatar answered Nov 16 '22 04:11

Alex Martelli


This is actually an old open Python issue:

  • Issue 1650090: doctest doesn't find nested functions

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

like image 4
Pi Delport Avatar answered Nov 16 '22 06:11

Pi Delport