Skip to content

Commit 195a12d

Browse files
committed
Add Test Explorer
Close #2430
1 parent db1c26f commit 195a12d

File tree

5 files changed

+27
-16
lines changed

5 files changed

+27
-16
lines changed
Loading
Loading
Loading
24.7 KB
Loading

docs/python/unit-testing.md

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ Area: python
44
TOCTitle: Unit Testing
55
ContentId: 9480bef3-4dfc-4671-a454-b9252567bc60
66
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
99
MetaSocialImage: images/tutorial/social.png
1010
---
11-
# Python unit tests in Visual Studio Code
11+
# Python unit testing in Visual Studio Code
1212

1313
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.
1414

@@ -175,22 +175,25 @@ Test discovery applies the discovery patterns for the current framework (which c
175175

176176
> **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.
177177
178+
If discovery succeeds, the status bar shows **Run Tests** instead:
179+
180+
![Status bar showing successful test discovery failed](images/unit-testing/discovery-succeeded-status-bar.png)
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+
![Status bar showing that test discovery failed](images/unit-testing/discovery-failed-status-bar.png)
185+
178186
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:
179187

180188
![Test adornments that appear in the VS Code editor for unittest code](images/unit-testing/editor-adornments-unittest.png)
181189

182190
![Test adornments that appear in the VS Code editor for pytest code](images/unit-testing/editor-adornments-pytest.png)
183191

184-
185192
> **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).
186193
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:
188195

189-
![Status bar showing that test discovery failed](images/unit-testing/discovery-failed-status-bar.png)
190-
191-
If discovery succeeds, the status bar shows **Run Tests** instead:
192-
193-
![Status bar showing successful test discovery failed](images/unit-testing/discovery-succeeded-status-bar.png)
196+
![The VS Code Test Explorer for Python unit tests](images/unit-testing/test-explorer.png)
194197

195198
## Run tests
196199

@@ -206,6 +209,16 @@ You run tests using any of the following actions:
206209

207210
![Test commands that appear after using the Run Tests status bar command](images/unit-testing/run-test-commands.png)
208211

212+
- In **Test Explorer**:
213+
214+
- To run all discovered tests, select the play button at the top of **Test Explorer**:
215+
216+
![Running all tests through Test Explorer](images/unit-testing/test-explorer-run-all-tests.png)
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+
![Running tests at specific scopes through Test Explorer](images/unit-testing/test-explorer-run-scoped-tests.png)
221+
209222
- Right-click a file in Explorer and select **Run All Unit Tests**, which runs the tests in that one file.
210223

211224
- 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:
223236
| Run Unit Test Method | Prompts for the name of a test to run, providing auto-completion for test names. |
224237
| Show Unit Test Output | Opens the Python Test Log panel with information about passing and failing tests, as well as errors and skipped tests. |
225238

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-
![Test result adornments on a unittest class](images/unit-testing/result-adornments-unittest.png)
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.
229240

230-
![Test result adornments on pytest methods](images/unit-testing/result-adornments-pytest.png)
241+
![Test results on a unittest class and in Test Explorer](images/unit-testing/test-results.png)
231242

232243
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):
233244

@@ -245,7 +256,7 @@ For example, the `test_decrement` functions given earlier are failing because th
245256

246257
1. Set a breakpoint on first the line in the `test_decrement` function.
247258

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.
249260

250261
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.
251262

@@ -263,7 +274,7 @@ For example, the `test_decrement` functions given earlier are failing because th
263274

264275
> **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!
265276

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 for all 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 for all tests and a single test method, respectively. You can also use the "bug" icons in **Test Explorer** to launch the debugger for all tests in a selected scope as well as all discovered tests.
267278

268279
The debugger works the same for unit tests as for 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.
269280

0 commit comments

Comments
 (0)