|
54 | 54 | "**Refactoring** \n", |
55 | 55 | "When we want to modify working code, since tests can give us some degree of confidence that we are not breaking the functionality, for example, while optimizing running time.\n", |
56 | 56 | "\n", |
57 | | - "\n", |
58 | 57 | "**Documentation** \n", |
59 | 58 | "Tests can serve as a way to communicate other people (our ourselves) the expected behaviour of our code." |
60 | 59 | ] |
|
129 | 128 | " return value * value\n", |
130 | 129 | "```\n", |
131 | 130 | "\n", |
132 | | - "So let's create a directory (next to `src`) called `tests` and make a file called `tests/test_square.py` including our previous `test_square` code:\n", |
| 131 | + "So let's create a directory (next to `src`) called `tests` and make a file called `tests/test_square.py` including our previous `test_square` code.\n", |
| 132 | + "\n", |
| 133 | + "If you've been following along, your project should look more or less like this:\n", |
| 134 | + "\n", |
| 135 | + "{width=150}\n", |
| 136 | + "\n", |
| 137 | + "Now put this content in `test_square.py`:\n", |
133 | 138 | "\n", |
134 | 139 | "```python\n", |
135 | 140 | "from pycourse.square import square # Import function from source code test\n", |
|
141 | 146 | " assert square(5) == 25\n", |
142 | 147 | "```\n", |
143 | 148 | "\n", |
144 | | - "Notice that we do not call the function, we just define it.\n", |
145 | 149 | "\n", |
146 | 150 | ":::{.callout-important}\n", |
| 151 | + "Notice that we do not call the function, we just define it.\n", |
147 | 152 | "The file must be called `test_*.py` or `*_test.py` for pytest to discover it and the unit tests must start with `test_`.\n", |
148 | 153 | ":::\n", |
149 | 154 | "\n", |
|
250 | 255 | "source": [ |
251 | 256 | "## Exercises\n", |
252 | 257 | "1) Modify the function `square` in `square.py` so that it returns `value * value * 2` and run the test again. Pay close attention to the error messages and the diffs!\n", |
253 | | - "2) Write a function called `reverse_str` that takes a string and returns the reversed version, eg \"hola\" -> \"aloh\". Write a unit test and make sure the tests pass. Think about covering corner cases in your tests.S\n", |
| 258 | + "2) Write a function called `reverse_str` that takes a string and returns the reversed version, eg \"hola\" -> \"aloh\". Write a unit test and make sure the tests pass. Think about covering corner cases in your tests.\n", |
254 | 259 | "3) Make the function `reverse_str` strict to only accept strings as input and throw an error if another type is passed. Write a unit test that covers both the \"happy path\" and the \"error path\"." |
255 | 260 | ] |
256 | 261 | } |
|
0 commit comments