-
-
Save nicoddemus/d100e3659db7fdddf691 to your computer and use it in GitHub Desktop.
Sample usage of the `mock` module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
============================= test session starts ============================= | |
platform win32 -- Python 2.7.6 -- pytest-2.5.1 -- D:\Programming\Python27\python.exe | |
plugins: xdist | |
collected 2 items | |
test_twitter.py:5: test_twitter PASSED | |
test_twitter.py:10: test_task_utils FAILED | |
================================== FAILURES =================================== | |
_______________________________ test_task_utils _______________________________ | |
def test_task_utils(): | |
with mock.patch('task_utils.mechanize_url') as mech: | |
mech.return_value = 'MOCKED' | |
> assert twitter.api() == 'MOCKED' | |
E assert 'ORIGINAL' == 'MOCKED' | |
E - ORIGINAL | |
E + MOCKED | |
test_twitter.py:13: AssertionError | |
===================== 1 failed, 1 passed in 0.02 seconds ====================== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def mechanize_url(): | |
return 'ORIGINAL' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mock | |
import twitter | |
import task_utils | |
def test_twitter(): | |
with mock.patch('twitter.mechanize_url') as mech: | |
mech.return_value = 'MOCKED' | |
assert twitter.api() == 'MOCKED' | |
def test_task_utils(): | |
with mock.patch('task_utils.mechanize_url') as mech: | |
mech.return_value = 'MOCKED' | |
assert twitter.api() == 'MOCKED' | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from task_utils import mechanize_url | |
def api(): | |
return mechanize_url() |
Here is the version of the mock module I'm using:
>>> import mock
>>> mock.__version__
'1.0.1'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Changing
twitter.py
to:Causes
test_twitter
to fail andtest_task_utils
to pass: