Skip to content

Instantly share code, notes, and snippets.

@matsjoyce
Created April 2, 2015 09:39
Show Gist options
  • Save matsjoyce/07f652d6cf4b3e548a13 to your computer and use it in GitHub Desktop.
Save matsjoyce/07f652d6cf4b3e548a13 to your computer and use it in GitHub Desktop.
Result of Override a “private” method in a python module
# module.py
def _cmd(command, args):
# do something nasty
print(command, args)
def function_to_be_tested():
# do cool things
_cmd('rm', '-rf /')
return 1
# test.py
from mock import patch
import module
def _cmd_mock(command, args):
# do nothing
pass
@patch('module._cmd', _cmd_mock)
def test_function():
assert module.function_to_be_tested() == 1
# result
$ python test.py
Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment