I have a body of python code that contains inline functions within functions. I'd like to unit test the make_exciting
inner function, so I'm trying to figure out how to invoke it directly.
def say_something_exciting(name, phrase):
def make_exciting(phrase):
return phrase + "!"
return "%s says '%s'" % (name, make_exciting(phrase))
Function say_something_exciting
is written at the top level of a .py file, and is not inside a class. The py file is in the org.something
module. Tried:
- Invoking the function directly via
org.something.say_something_exciting.make_exciting("Hello")
- error: 'function' object has no attribute 'make_exciting' - Inspecting
dir(org.something.say_something_exciting)
andorg.something.say_something_exciting.__dict__
for any paths to traverse, didn't seemake_exciting
anywhere. internal_function = org.something.say_something_exciting.__dict__.get('make_exciting')
, butinternal_function
isNone
.
How can I access (unit test) this inner function? This may suggest what I'm asking isn't possible. I'm generally familiar with unit testing and how to use the unittest
module; accessing the function is the problem. If it's not possible, how should I re-write this code to support testing (if other than promote the inner function to a top-level function)?. Thanks!
UPDATE: In Java I often give class methods default/package visibility so they're less visible but still accessible to unit tests, looking for a python equivalent.