Function defaults are set and stored with the function object when the function definition is executed.
Mocking platform.machine
works fine, but the default value for the arch
argument has long since been set by calling platform.machine()
and using the return value. The expression is not used when test()
is called.
See "Least Astonishment" in Python: The Mutable Default Argument for why that is.
You'll need to patch platform
before you ever import the module that defines the function; you could move the function to a new module, and do this:
import sys
if 'modulename' in sys.modules:
del sys.modules['modulename'] # ensure the cached module is cleared
with mock.patch.object(platform, "machine", return_value="TEST"):
from modulename import sys
test()
del sys.modules['modulename'] # clear the module with the mocked value again
This is rather cumbersome and will fail if you were to run tests in threads.
You can use None
as the default instead and create the default when test
is called instead:
def test(arch=None):
if arch is None:
arch = platform.machine()
print "arch = %s" % arch
print "machine = %s" % platform.machine()