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
Copy file name to clipboardExpand all lines: docs/python/unit-testing.md
+27-16Lines changed: 27 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -4,11 +4,11 @@ Area: python
4
4
TOCTitle: Unit Testing
5
5
ContentId: 9480bef3-4dfc-4671-a454-b9252567bc60
6
6
PageTitle: Unit Testing Python in Visual Studio Code
7
-
DateApproved: 02/19/2019
8
-
MetaDescription: Unit Testing Python in Visual Studio Code
7
+
DateApproved: 03/07/2019
8
+
MetaDescription: Unit Testing Python in Visual Studio Code including the Test Explorer
9
9
MetaSocialImage: images/tutorial/social.png
10
10
---
11
-
# Python unit tests in Visual Studio Code
11
+
# Python unit testing in Visual Studio Code
12
12
13
13
The [Python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python) supports unit testing with Python's built-in [unittest](https://docs.python.org/3/library/unittest.html) framework as well as [pytest](https://docs.pytest.org/en/latest/). [Nose](https://nose.readthedocs.io/en/latest/) is also supported, although the framework itself is in maintenance mode.
14
14
@@ -175,22 +175,25 @@ Test discovery applies the discovery patterns for the current framework (which c
175
175
176
176
> **Tip**: Sometimes unit tests placed in subfolders aren't discovered because such test files cannot be imported. To make them importable, create an empty file named `__init__.py` in that folder.
177
177
178
+
If discovery succeeds, the status bar shows **Run Tests** instead:
179
+
180
+

181
+
182
+
If discovery fails (for example, the test framework isn't installed), you see a notification on the status bar. Selecting the notification provides more information:
183
+
184
+

185
+
178
186
Once VS Code recognizes tests, it provides several ways to run those tests as described in [Run tests](#run-tests). The most obvious means are CodeLens adornments that appear directly in the editor and allow you to easily run a single test method or, with unittest, a test class:
179
187
180
188

181
189
182
190

183
191
184
-
185
192
> **Note**: At present, the Python extension doesn't provide a setting to turn the adornments on or off. To suggest a different behavior, file an issue on the [vscode-python repository](https://github.com/Microsoft/vscode-python/issues).
186
193
187
-
If discovery fails (for example, the test framework isn't installed), you see a notification on the status bar. Selecting the notification provides more information:
194
+
For Python, test discovery also activates the **Test Explorer** with an icon on the VS Code activity bar. The **Test Explorer** helps you visualize, navigate, and run unit tests:
188
195
189
-

190
-
191
-
If discovery succeeds, the status bar shows **Run Tests** instead:
192
-
193
-

196
+

194
197
195
198
## Run tests
196
199
@@ -206,6 +209,16 @@ You run tests using any of the following actions:
206
209
207
210

208
211
212
+
- In **Test Explorer**:
213
+
214
+
- To run all discovered tests, select the play button at the top of **Test Explorer**:
215
+
216
+

217
+
218
+
- To run a specific group of tests, or a single test, select the file, class, or test, then select the play button to the right of that item:
219
+
220
+

221
+
209
222
- Right-click a file in Explorer and select **Run All Unit Tests**, which runs the tests in that one file.
210
223
211
224
- From the **Command Palette**, select any of the run unit test commands:
@@ -223,11 +236,9 @@ You run tests using any of the following actions:
223
236
| Run Unit Test Method | Prompts for the name of a test to run, providing auto-completion for test names. |
224
237
| Show Unit Test Output | Opens the Python Test Log panel with information about passing and failing tests, as well as errors and skipped tests. |
225
238
226
-
After a test run, VS Code displays results directly with the CodeLens adornments in the editor. Here, you can see that that one test passed and one failed. In the case of unittest, you can also see that the test class as a whole failed because at least one of its tests failed. In the case of pytest, failed tests are also adorned with a red underline.
227
-
228
-

239
+
After a test run, VS Code displays results directly with the CodeLens adornments in the editor and in **Test Explorer**. Results are shown both for individual tests as well as any classes and files containing those tests. Failed tests are also adorned in the editor with a red underline.
229
240
230
-

241
+

231
242
232
243
VS Code also shows test results in the **Python Test Log** output panel (use the **View** > **Output** menu command to show the **Output** panel, then select **Python Test Log** from the drop-down on the right side):
233
244
@@ -245,7 +256,7 @@ For example, the `test_decrement` functions given earlier are failing because th
245
256
246
257
1. Set a breakpoint on first the line in the `test_decrement` function.
247
258
248
-
1. Select the **Debug Test** adornment above that function. VS Code starts the debugger and pauses at the breakpoint.
259
+
1. Select the **Debug Test** adornment above that function or the "bug" icon for that test in **Test Explorer**. VS Code starts the debugger and pauses at the breakpoint.
249
260
250
261
1. In the **Debug Console** panel, enter `inc_dec.decrement(3)` to see that the actual result is 2, whereas the expected result specified in the test is the incorrect value of 4.
251
262
@@ -263,7 +274,7 @@ For example, the `test_decrement` functions given earlier are failing because th
263
274
264
275
>**Note**: running or debugging a unit test does not automatically save the test file. Always be sure to save changes to a test before running it, otherwise you'll likely be confused by the results because they still reflect the previous version of the file!
265
276
266
-
The **Python: Debug All Tests**and**Python: Debug Unit Test Method** commands (on both the Command Palette and Status Bar menu) launch the debugger forall tests and a single test method, respectively.
277
+
The **Python: Debug All Tests**and**Python: Debug Unit Test Method** commands (on both the Command Palette and Status Bar menu) launch the debugger forall tests and a single test method, respectively. You can also use the "bug" icons in**Test Explorer** to launch the debugger forall tests in a selected scope as well asall discovered tests.
267
278
268
279
The debugger works the same for unit tests asfor other Python code, including breakpoints, variable inspection, and so on. For more information, see [Python debugging configurations](/docs/python/debugging.md) and the general VS Code [Debugging](/docs/editor/debugging.md) article.
0 commit comments