Last active
September 3, 2021 16:39
-
-
Save themarcelor/15dbb6f1510b2550649d7dbe651a0b53 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| 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 |
This file contains hidden or 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
| 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