Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you unit test a nested function? [duplicate]

How would you unit test the nested function f1() in the following example?

def f():
    def f1():
       return 1
    return 2

Or should functions that need testing not be nested?

like image 604
e1i45 Avatar asked Nov 28 '12 13:11

e1i45


2 Answers

There is a similar question in this link. But short answer: you can't access an inner function from an outer element.

For testing purposes, maybe an alternative would be to change the inner function for a private outer one?

like image 82
alemangui Avatar answered Oct 12 '22 13:10

alemangui


You don't, because you can't.

You will have to either limit your unit testing to the outer function, or you move the inner function elsewhere.

like image 20
Martijn Pieters Avatar answered Oct 12 '22 13:10

Martijn Pieters