I need to unit-test a python module by changing a default in the head of the application module called process.py. The declared default is a fixed int. I could change it to use something else from os.environ, but I was hoping to simply assign the global, but clearly I am missing some comprehension around 'def'.
process.default_timeout_secs = 2
# --name: process
default_timeout_secs = 120
def call_process(cmd, timeout=default_timeout_secs):
print 'run:', cmd, 'timeout:', timeout
...
# --name: test_process
from nose.tools import *
import process
@ raises(TimeoutExpired)
def test_process_run_modified_default():
process.default_timeout_secs = 5
run(sleep 10)
I understand from other posts, that the process.call_process.func_defaults value for default_timeout_secs is not the one in the top of the module when the module is imported. How do I change the default used in the function?
process.default_timeout_secs = 5 process.call_process.func_globals['default_timeout'] 5 process.call_process.func_defaults (120)
Out[21]: 5
>>> process.call_process(cmd)
Out[22]: call_process: cmd= sleep 2 timeout= 120 <----- 120?
Executing cmd sleep 2 verbose=True
The output should show exception TimoutExpired.