|
9 | 9 | "\n",
|
10 | 10 | "### Topics\n",
|
11 | 11 | "\n",
|
12 |
| - "- What is Mock?\n", |
| 12 | + "- the motivation behind mocking/facking/patching objects\n", |
| 13 | + "- What is a Magic Mock?\n", |
13 | 14 | "- use Mock to imitate objects in your tests\n",
|
14 | 15 | "- check usage data to understand how you use your objects\n",
|
15 | 16 | "- customize your mock objects' return values and side effects\n",
|
16 | 17 | "- patch() objects throughout your codebase\n",
|
17 | 18 | "- common mock problems and avoiding them\n",
|
18 | 19 | "\n",
|
19 |
| - "## Imitating objects using Mocks\n", |
| 20 | + "## Motivation\n", |
20 | 21 | "\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", |
22 | 22 | "- isolated problems are easier to diagnose and solve\n",
|
23 | 23 | "- if a test fails, diagnosing/debugging many interrelated components can be very difficult\n",
|
24 | 24 | " - e.g., why the engine of a gasoline car not firing\n",
|
|
27 | 27 | " 1. isolate a unit under test - create collaborating classes and functions so we can test one unknown component\n",
|
28 | 28 | " 2. test code that requires an object that is either expensive or risky to use;\n",
|
29 | 29 | " - 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?" |
39 | 36 | ]
|
40 | 37 | },
|
41 | 38 | {
|
|
92 | 89 | "stdout.close()"
|
93 | 90 | ]
|
94 | 91 | },
|
| 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 | + }, |
95 | 110 | {
|
96 | 111 | "cell_type": "code",
|
97 | 112 | "execution_count": 12,
|
|
121 | 136 | "stdout.write('Back to the console\\n')"
|
122 | 137 | ]
|
123 | 138 | },
|
| 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 | + }, |
124 | 158 | {
|
125 | 159 | "cell_type": "code",
|
126 | 160 | "execution_count": null,
|
|
1807 | 1841 | ],
|
1808 | 1842 | "metadata": {
|
1809 | 1843 | "kernelspec": {
|
1810 |
| - "display_name": "Python 3", |
| 1844 | + "display_name": "Python 3 (ipykernel)", |
1811 | 1845 | "language": "python",
|
1812 | 1846 | "name": "python3"
|
1813 | 1847 | },
|
|
1821 | 1855 | "name": "python",
|
1822 | 1856 | "nbconvert_exporter": "python",
|
1823 | 1857 | "pygments_lexer": "ipython3",
|
1824 |
| - "version": "3.12.1" |
| 1858 | + "version": "3.10.8" |
1825 | 1859 | }
|
1826 | 1860 | },
|
1827 | 1861 | "nbformat": 4,
|
|
0 commit comments