Skip to content

Commit 767f75c

Browse files
committed
update MockingPatching
1 parent 1f3b632 commit 767f75c

File tree

1 file changed

+48
-14
lines changed

1 file changed

+48
-14
lines changed

notebooks/TestingMockingPatching.ipynb

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
"\n",
1010
"### Topics\n",
1111
"\n",
12-
"- What is Mock?\n",
12+
"- the motivation behind mocking/facking/patching objects\n",
13+
"- What is a Magic Mock?\n",
1314
"- use Mock to imitate objects in your tests\n",
1415
"- check usage data to understand how you use your objects\n",
1516
"- customize your mock objects' return values and side effects\n",
1617
"- patch() objects throughout your codebase\n",
1718
"- common mock problems and avoiding them\n",
1819
"\n",
19-
"## Imitating objects using Mocks\n",
20+
"## Motivation\n",
2021
"\n",
21-
"- based on: [https://realpython.com/python-mock-library/](https://realpython.com/python-mock-library/) and [https://docs.python.org/3/library/unittest.mock.html](https://docs.python.org/3/library/unittest.mock.html)\n",
2222
"- isolated problems are easier to diagnose and solve\n",
2323
"- if a test fails, diagnosing/debugging many interrelated components can be very difficult\n",
2424
" - e.g., why the engine of a gasoline car not firing\n",
@@ -27,15 +27,12 @@
2727
" 1. isolate a unit under test - create collaborating classes and functions so we can test one unknown component\n",
2828
" 2. test code that requires an object that is either expensive or risky to use;\n",
2929
" - things like shared databases, filesystems, and cloud infrastructures can be very expensive to setup and tear down for testing\n",
30-
"- `unittest.mock` module provides Mock base class for mocking objects\n",
31-
"- you can pass mock objects as arguments to functions\n",
32-
"- assign/patch other objects\n",
33-
"- when substituting an object in your code, the Mock must look like the real object it is replacing\n",
34-
" - mock objects must have the same members (attributes and methods) that are being tested\n",
35-
" - e.g., if you're mocking `json` library and your program calls `dumps()`, then the mock object must also contain `dumps()` \n",
36-
"- Mock must simulate any object that it replaces\n",
37-
" - Mock creates attributes/members when you access them dynamically!\n",
38-
"- Mock methods can take whatever arguments you provide but always return Mock object"
30+
"\n",
31+
"### Problems\n",
32+
"\n",
33+
"- two immediate problems we've been facing in solving Kattis problems are:\n",
34+
"1. how can we programmatically assert what result the function printed to standard output?\n",
35+
"2. how to automate data from standard input with out manually entering the input?"
3936
]
4037
},
4138
{
@@ -92,6 +89,24 @@
9289
"stdout.close()"
9390
]
9491
},
92+
{
93+
"cell_type": "code",
94+
"execution_count": 1,
95+
"id": "bb11055d",
96+
"metadata": {},
97+
"outputs": [
98+
{
99+
"name": "stdout",
100+
"output_type": "stream",
101+
"text": [
102+
"This is a log file\r\n"
103+
]
104+
}
105+
],
106+
"source": [
107+
"! cat log.txt"
108+
]
109+
},
95110
{
96111
"cell_type": "code",
97112
"execution_count": 12,
@@ -121,6 +136,25 @@
121136
"stdout.write('Back to the console\\n')"
122137
]
123138
},
139+
{
140+
"cell_type": "markdown",
141+
"id": "4e856508",
142+
"metadata": {},
143+
"source": [
144+
"## Imitating objects using Mocks\n",
145+
"\n",
146+
"- based on: [https://realpython.com/python-mock-library/](https://realpython.com/python-mock-library/) and [https://docs.python.org/3/library/unittest.mock.html](https://docs.python.org/3/library/unittest.mock.html)\n",
147+
"- `unittest.mock` module provides Mock base class for mocking objects\n",
148+
"- you can pass mock objects as arguments to functions\n",
149+
"- assign/patch other objects\n",
150+
"- when substituting an object in your code, the Mock must look like the real object it is replacing\n",
151+
" - mock objects must have the same members (attributes and methods) that are being tested\n",
152+
" - e.g., if you're mocking `json` library and your program calls `dumps()`, then the mock object must also contain `dumps()` \n",
153+
"- Mock must simulate any object that it replaces\n",
154+
" - Mock creates attributes/members when you access them dynamically!\n",
155+
"- Mock methods can take whatever arguments you provide but always return Mock object"
156+
]
157+
},
124158
{
125159
"cell_type": "code",
126160
"execution_count": null,
@@ -1807,7 +1841,7 @@
18071841
],
18081842
"metadata": {
18091843
"kernelspec": {
1810-
"display_name": "Python 3",
1844+
"display_name": "Python 3 (ipykernel)",
18111845
"language": "python",
18121846
"name": "python3"
18131847
},
@@ -1821,7 +1855,7 @@
18211855
"name": "python",
18221856
"nbconvert_exporter": "python",
18231857
"pygments_lexer": "ipython3",
1824-
"version": "3.12.1"
1858+
"version": "3.10.8"
18251859
}
18261860
},
18271861
"nbformat": 4,

0 commit comments

Comments
 (0)