Skip to content

Instantly share code, notes, and snippets.

@themarcelor
Last active September 3, 2021 16:39
Show Gist options
  • Select an option

  • Save themarcelor/15dbb6f1510b2550649d7dbe651a0b53 to your computer and use it in GitHub Desktop.

Select an option

Save themarcelor/15dbb6f1510b2550649d7dbe651a0b53 to your computer and use it in GitHub Desktop.
import mock
class MyDatabaseAccessModule():
def fetch_data_from_db(self):
print('this one will also not be printed')
return "the data"
class MyHTTPAPIModule():
def fetch_data(self):
print('this will never be printed')
myDatabaseAccessObjInstance = MyDatabaseAccessModule()
return myDatabaseAccessObjInstance.fetch_data_from_db()
@mock.patch("test_awkward.MyHTTPAPIModule.fetch_data", mock.MagicMock(return_value="the data"))
def test_fetch_data():
instanceOfTheRealMyHTTPAPIModuleObject = MyHTTPAPIModule()
# the real function is not called here
result = instanceOfTheRealMyHTTPAPIModuleObject.fetch_data()
assert result == "the data"
# save this file and run
# pytest -vv -s test_awkward.py
class MyMathClass():
def sum_of_two_numbers(self, a, b):
return a+b
def test_sum_of_two_numbers():
myMathObjInstance = MyMathClass()
result = myMathObjInstance.sum_of_two_numbers(1,1)
assert result == 2
# save this file and run
# pytest -vv -s test_sum.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment