Skip to content

Instantly share code, notes, and snippets.

@durgaswaroop
Last active April 12, 2020 17:32
Show Gist options
  • Save durgaswaroop/4567248b74da6f6a271c418f92ec9b4a to your computer and use it in GitHub Desktop.
Save durgaswaroop/4567248b74da6f6a271c418f92ec9b4a to your computer and use it in GitHub Desktop.
Mocking with Pytest-mock articles
# application1.py
from time import sleep
def is_windows():
# This sleep could be some complex operation instead
sleep(5)
return True
def get_operating_system():
return 'Windows' if is_windows() else 'Linux'
# test_application1a.py
from application1 import get_operating_system
def test_get_operating_system(mocker):
mocker.patch('application1.is_windows', return_value=True)
assert get_operating_system() == 'Windows'
# test_application1b.py
from application1 import get_operating_system
def test_get_operation_system_is_linux(mocker):
mocker.patch('application1.is_windows', return_value=False)
assert get_operating_system() == 'Linux'
@durgaswaroop
Copy link
Author

durgaswaroop commented Apr 12, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment