You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -185,36 +185,54 @@ Debuggers allow us to peer at the internal workings of a program,
185
185
such as variables and other state,
186
186
as it performs its functions.
187
187
188
-
### Running Tests in IDE
188
+
Firstly, to make it easier to track what's going on, we can set up our IDE to run and debug our tests instead of running them from the command line.
189
189
190
-
Firstly, to make it easier to track what's going on, we can set up our IDE to run and debug our tests
191
-
instead of running them from the command line.
190
+
### Configuring the Test Framework
192
191
193
192
::: group-tab
194
193
195
194
### PyCharm
196
195
197
-
If you have not done so already,
198
-
you will first need to enable the Pytest framework in PyCharm.
196
+
If you have not done so already, you will first need to enable the Pytest framework in PyCharm.
199
197
You can do this by:
200
198
201
-
1. Select either `PyCharm` > `Preferences` (Mac) or `File` > `Settings` (Linux, Windows).
202
-
2. Then, in the preferences window that appears,
203
-
select `Tools` -> `Python integrated tools` > from the left.
199
+
1. Select either `PyCharm` > `Preferences` (Mac) or `File -> Settings` (Linux, Windows).
200
+
2. Then, in the preferences window that appears, select `Tools -> Python integrated tools` from the left.
204
201
3. Under `Testing`, for `Default test runner` select `pytest`.
205
202
4. Select `OK`.
206
203
207
204
{alt='Setting up test framework in PyCharm' .image-with-shadow width="1000px"}
208
205
209
-
We can now run `pytest` over our tests in PyCharm,
210
-
similarly to how we ran our `inflammation-analysis.py` script before.
211
-
Right-click the `test_models.py` file
212
-
under the `tests` directory in the file navigation window on the left,
213
-
and select `Run 'pytest in test_model...'`.
206
+
207
+
### VS Code
208
+
209
+
If you have not done so already, you will first need to confugure the Pytest framework in VS Code.
210
+
Open the Test Explorer view (click on the test beaker icon on the VS Code Activity Bar on the left hand side).
211
+
212
+
You should see a `Configure Python Tests` button if a test framework is not enabled.
213
+
Clicking on it prompts you to select a test framework and a folder containing your tests.
214
+
215
+
{alt='Setting up test framework in VS Code' .image-with-shadow width="1000px"}
216
+
217
+
{alt='Setting up test framework in VS Code' .image-with-shadow width="1000px"}
218
+
219
+
Tests can be configured anytime by using the `Python: Configure Tests` command from the Command Palette
220
+
or by setting `python.testing.pytestEnabled` in the Settings editor or `settings.json` file (described in the VS Code Settings in the [episode on IDEs](/13-ides.md)).
221
+
Each testing framework also has further specific configuration settings as described in the [Test configuration settings of the VS Code documentation for Python](https://code.visualstudio.com/docs/python/testing#_test-configuration-settings).
222
+
223
+
:::
224
+
225
+
### Running the Tests
226
+
227
+
We can now run `pytest` over our tests in the IDE, similarly to how we ran our `inflammation-analysis.py` script before.
228
+
229
+
::: group-tab
230
+
231
+
### PyCharm
232
+
233
+
Right-click the `test_models.py` file under the `tests` directory in the file navigation window on the left, and select `Run 'pytest in test_model...'`.
214
234
You'll see the results of the tests appear in PyCharm in a bottom panel.
215
-
If you scroll down in that panel you should see
216
-
the failed `test_patient_normalise()` test result
217
-
looking something like the following:
235
+
If you scroll down in that panel you should see the failed `test_patient_normalise()` test result looking something like the following:
218
236
219
237
{alt='Running pytest in PyCharm' .image-with-shadow width="1000px"}
220
238
@@ -239,45 +257,41 @@ so select any others you see and click the `-` button at the top to remove them.
239
257
This will avoid any confusion when running our tests separately.
240
258
Click `OK` when done.
241
259
260
+
Note that, whenever a Python program prints text to the terminal or to a file, it first stores this text in an **output buffer**.
261
+
When the buffer becomes full or is **flushed**, the contents of the buffer are written to the terminal / file in one go and the buffer is cleared.
262
+
This is usually done to increase performance by effectively converting multiple output operations into just one.
263
+
Printing text to the terminal is a relatively slow operation, so in some cases this can make quite a big difference to the total execution time of a program.
264
+
265
+
However, using buffered output can make debugging more difficult, as we can no longer be quite sure when a log message will be displayed.
266
+
In order to make debugging simpler, PyCharm automatically adds the environment variable `PYTHONUNBUFFERED` we see in the screenshot above, which disables output buffering.
267
+
268
+
Now, if you select the green arrow next to a test function in our `test_models.py` script in PyCharm, and select `Run 'pytest in test_model...'`, we can run just that test:
269
+
270
+
{alt='Running a single test in PyCharm' .image-with-shadow width="800px"}
271
+
272
+
Click on the "run" button next to `test_patient_normalise`, and you will be able to see that PyCharm runs just that test function, and we see the same `AssertionError` that we saw before.
273
+
242
274
### VS Code
243
275
244
-
:::
276
+
In VS Code - one you have configured the test framework to use - you can run tests either from the Project Explorer or Test Explorer in the VS Code Activity Bar.
245
277
246
-
::::::::::::::::::::::::::::::::::::::::: callout
278
+
From the Project Explorer, right-click on the test folder and select "Run Tests".
247
279
248
-
## Buffered Output
280
+
{alt='Running pytest in VS Code from the Project Explorer' .image-with-shadow width="1000px"}
249
281
250
-
Whenever a Python program prints text to the terminal or to a file,
251
-
it first stores this text in an **output buffer**.
252
-
When the buffer becomes full or is **flushed**,
253
-
the contents of the buffer are written to
254
-
the terminal / file in one go and the buffer is cleared.
255
-
This is usually done to increase performance
256
-
by effectively converting multiple output operations into just one.
257
-
Printing text to the terminal is a relatively slow operation,
258
-
so in some cases this can make quite a big difference
259
-
to the total execution time of a program.
282
+
Alternatively, from the Test Explorer view in VS Code (click on the test beaker icon on the VS Code Activity Bar on the left hand side) - locate `python-intermediate-inflammation` and hover over it.
283
+
You will see a few "run" buttons next to it - click either the play button (`Run Test`) or the play button with the little check (`Run Test with Coverage`).
260
284
261
-
However, using buffered output can make debugging more difficult,
262
-
as we can no longer be quite sure when a log message will be displayed.
263
-
In order to make debugging simpler,
264
-
PyCharm automatically adds the environment variable `PYTHONUNBUFFERED`
265
-
we see in the screenshot above,
266
-
which disables output buffering.
285
+
{alt='Running pytest in VS Code from the Test Explorer' .image-with-shadow width="1000px"}
267
286
287
+
In either case, you should get the test results on the test results pane at the bottom of the window and you should see the same `AssertionError` that we saw before from the command line.
Once we have run all our tests - you can run an individual (single) test from the test result pane or from the Test Explorer view (e.g. just the failing test) without rerunning all.
270
290
271
-
Now, if you select the green arrow next to a test function
272
-
in our `test_models.py` script in PyCharm,
273
-
and select `Run 'pytest in test_model...'`,
274
-
we can run just that test:
291
+
{alt='Running individual test Test Explorer in VS Code' .image-with-shadow width="1000px"}
275
292
276
-
{alt='Running a single test in PyCharm' .image-with-shadow width="800px"}
293
+
:::
277
294
278
-
Click on the "run" button next to `test_patient_normalise`,
279
-
and you will be able to see that PyCharm runs just that test function,
280
-
and we see the same `AssertionError` that we saw before.
281
295
282
296
### Running the Debugger
283
297
@@ -286,39 +300,27 @@ what is happening inside the `patient_normalise` function.
286
300
To do this we will add a *breakpoint* in the code.
287
301
A breakpoint will pause execution at that point allowing us to explore the state of the program.
288
302
289
-
To set a breakpoint, navigate to the `models.py` file
290
-
and move your mouse to the `return` statement of the `patient_normalise` function.
291
-
Click to just to the right of the line number for that line
292
-
and a small red dot will appear,
293
-
indicating that you have placed a breakpoint on that line.
303
+
To set a breakpoint, navigate to the `models.py` file and move your mouse to the `return` statement of the `patient_normalise` function.
304
+
Click to just to the right of the line number for that line and a small red dot will appear, indicating that you have placed a breakpoint on that line.
305
+
306
+
::: group-tab
307
+
308
+
### PyCharm
294
309
295
310
{alt='Setting a breakpoint in PyCharm' .image-with-shadow width="600px"}
296
311
297
-
Now if you select the green arrow next to the `test_patient_normalise` function
298
-
and instead select `Debug 'pytest in test_model...'`,
299
-
you will notice that execution will be paused
300
-
at the `return` statement of `patient_normalise`.
301
-
In the debug panel that appears below,
302
-
we can now investigate the exact state of the program
303
-
prior to it executing this line of code.
312
+
Now if you select the green arrow next to the `test_patient_normalise` function and instead select `Debug 'pytest in test_model...'`,
313
+
you will notice that execution will be paused at the `return` statement of `patient_normalise`.
314
+
In the debug panel that appears below, we can now investigate the exact state of the program prior to it executing this line of code.
304
315
305
-
In the debug panel below,
306
-
in the `Debugger` tab you will be able to see
307
-
two sections that looks something like the following:
316
+
In the debug panel below, in the `Debugger` tab you will be able to see two sections that looks something like the following:
308
317
309
318
{alt='Debugging in PyCharm' .image-with-shadow width="1000px"}
310
319
311
-
- The `Frames` section on the left,
312
-
which shows the **call stack**
313
-
(the chain of functions that have been executed to lead to this point).
314
-
We can traverse this chain of functions if we wish,
315
-
to observe the state of each function.
316
-
- The `Variables` section on the right,
317
-
which displays the local and global variables currently in memory.
318
-
You will be able to see the `data` array
319
-
that is input to the `patient_normalise` function,
320
-
as well as the `max` local array
321
-
that was created to hold the maximum inflammation values for each patient.
320
+
- The `Frames` section on the left, which shows the **call stack** (the chain of functions that have been executed to lead to this point).
321
+
We can traverse this chain of functions if we wish, to observe the state of each function.
322
+
- The `Variables` section on the right, which displays the local and global variables currently in memory.
323
+
You will be able to see the `data` array that is input to the `patient_normalise` function, as well as the `max` local array that was created to hold the maximum inflammation values for each patient.
322
324
323
325
We also have the ability run any Python code we wish at this point
324
326
to explore the state of the program even further!
@@ -332,9 +334,22 @@ in the return line of the function.
332
334
333
335
{alt='Debugging in PyCharm' .image-with-shadow width="1000px"}
334
336
335
-
Now, looking at the `max` variable,
336
-
we can see that something looks wrong,
337
-
as the maximum values for each patient do not correspond to the `data` array.
337
+
### VS Code
338
+
339
+
{alt='Setting a breakpoint in VS Code' .image-with-shadow width="600px"}
340
+
341
+
Now if you select the `test_patient_normalise` function in the Test Explorer and instead select `Debug Test` option,
342
+
you will notice that execution will be paused at the `return` statement of `patient_normalise`.
343
+
In the debug panel that appears below, we can now investigate the exact state of the program prior to it executing this line of code.
344
+
345
+
{alt='Debugging in VS Code - Variable values' .image-with-shadow width="1000px"}
346
+
347
+
The `Variables` section on the left displays the local and global variables currently in memory.
348
+
You will be able to see the `data` array that is input to the `patient_normalise` function, as well as the `max` local array that was created to hold the maximum inflammation values for each patient.
349
+
350
+
:::
351
+
352
+
Now, looking at the `max` variable, we can see that something looks wrong, as the maximum values for each patient do not correspond to the `data` array.
338
353
Recall that the input `data` array we are using for the function is
339
354
340
355
```python
@@ -343,26 +358,38 @@ Recall that the input `data` array we are using for the function is
343
358
[7, 8, 9]]
344
359
```
345
360
346
-
So the maximum inflammation for each patient should be `[3, 6, 9]`,
347
-
whereas the debugger shows `[7, 8, 9]`.
348
-
You can see that the latter corresponds exactly to the last column of `data`,
349
-
and we can immediately conclude that
350
-
we took the maximum along the wrong axis of `data`.
351
-
Now we have our answer,
352
-
stop the debugging process by selecting
353
-
the red square at the top right of the main PyCharm window.
354
-
355
-
So to fix the `patient_normalise` function in `models.py`,
356
-
change `axis=0` in the first line of the function to `axis=1`.
357
-
With this fix in place,
358
-
running all the tests again should result in all tests passing.
359
-
Navigate back to `test_models.py` in PyCharm,
360
-
right click `test_models.py`
361
-
and select `Run 'pytest in test_model...'`.
362
-
You should be rewarded with:
361
+
So the maximum inflammation for each patient should be `[3, 6, 9]`, whereas the debugger shows `[7, 8, 9]`.
362
+
You can see that the latter corresponds exactly to the last column of `data`, and we can immediately conclude that we took the maximum along the wrong axis of `data`.
363
+
Now we have our answer, stop the debugging process.
364
+
365
+
::: group-tab
366
+
367
+
### PyCharm
368
+
369
+
In PyCharm you can stop debugging by selecting the red square at the top right of the main PyCharm window.
370
+
371
+
### VS Code
372
+
373
+
In VS Code you can stop debugging by selecting `Run -> Stop Debugging` from the top menu.
374
+
375
+
:::
376
+
377
+
So to fix the `patient_normalise` function in `models.py`, change `axis=0` in the first line of the function to `axis=1`.
378
+
With this fix in place, running all the tests again should result in all tests passing.
379
+
380
+
::: group-tab
381
+
382
+
### PyCharm
363
383
364
384
{alt='All tests in PyCharm are successful' .image-with-shadow width="1000px"}
365
385
386
+
387
+
### VS Code
388
+
389
+
{alt='All tests in VS Code are successful' .image-with-shadow width="1000px"}
390
+
391
+
:::
392
+
366
393
::::::::::::::::::::::::::::::::::::::::: callout
367
394
368
395
## NumPy Axis
@@ -786,6 +813,12 @@ since this wouldn't work in the test case that uses a string.
786
813
787
814
If you do the challenge, again, be sure to commit your changes and push them to GitHub.
0 commit comments