I'd like to test the default behavior of a function. I have the following:
# app/foo.py
DEFAULT_VALUE = 'hello'def bar(text=DEFAULT_VALUE):print(text)
# test/test_app.py
import appdef test_app(monkeypatch):monkeypatch.setattr('app.foo.DEFAULT_VALUE', 'patched')app.foo.bar()assert 0
Output is hello
; not what I wanted.
One solution is to pass the default value explicitly: app.foo.bar(text=app.foo.DEFAULT_VALUE)
.
But I find it interesting that this doesn't seem to be an issue when defaulting to the global scope:
# app/foo.py
DEFAULT_VALUE = 'hello'def bar():print(DEFAULT_VALUE)
Output is patched
.
Why does this happen? And is there a better solution than passing the default explicitly?