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/testing.md
+12-10Lines changed: 12 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,7 @@ The [Python extension](https://marketplace.visualstudio.com/items?itemName=ms-py
16
16
17
17
(If you're already familiar with unit testing, you can skip to the [walkthroughs](#example-test-walkthroughs).)
18
18
19
-
A **unit** is a specific piece of code to be tested, such as a function or a class. **Unit tests** are then other pieces of code that specifically exercise the code unit with a full range of different inputs, including boundary and edge cases.
19
+
A **unit** is a specific piece of code to be tested, such as a function or a class. **Unit tests** are then other pieces of code that specifically exercise the code unit with a full range of different inputs, including boundary and edge cases. Both the unittest and pytest frameworks can be used to write unit tests.
20
20
21
21
For example, say you have a function to validate the format of an account number that a user enters in a web form:
22
22
@@ -88,15 +88,15 @@ With this code, you can experience working with tests in VS Code as described in
88
88
89
89
## Configure tests
90
90
91
-
Once you have the Python extension installed and a Python file open within the editor, a test beaker icon will be displayed on the VS Code Activity bar. The beaker icon is for the **Test Explorer** view. When opening the Test Explorer, you will see a **Configure Tests** button if you don't have a test framework enabled. Once you select **Configure Tests**, you will be prompted to select a test framework and a folder containing the tests. If you're using unittest, you will also be asked to select the file glob pattern used to identify your test files.
91
+
Once you have the Python extension installed and a Python file open within the editor, a test beaker icon will be displayed on the VS Code Activity bar. The beaker icon is for the **Test Explorer** view. When opening the Test Explorer, you will see a **Configure Tests** button if you don't have a test framework enabled. Once you select **Configure Tests**, you will be prompted to select a test framework and a folder containing the tests. If you're using unittest, you will also be asked to select the file glob pattern used to identify your test files. (a file glob pattern is a defined string pattern that matches file or folder names based on wildcards to then include or not include.)
92
92
93
93

94
94
95
-
You can configure your tests anytime by using the **Python: Configure Tests** command from the [Command Palette](/docs/getstarted/userinterface.md#command-palette). You can also configure testing manually by setting either `python.testing.unittestEnabled` or `python.testing.pytestEnabled`to true. Each framework also has specific configuration settings as described under [Test configuration settings](#test-configuration-settings) for their folders and patterns.
95
+
You can configure your tests anytime by using the **Python: Configure Tests** command from the [Command Palette](/docs/getstarted/userinterface.md#command-palette). You can also configure testing manually by setting either `python.testing.unittestEnabled` or `python.testing.pytestEnabled`which can be done either in the settings editor or in the `settings.json` file as described [here](https://code.visualstudio.com/docs/getstarted/settings). Each framework also has specific configuration settings as described under [Test configuration settings](#test-configuration-settings) for their folders and patterns.
96
96
97
97
If both frameworks are enabled, then the Python extension will only run `pytest`.
98
98
99
-
When you enable a test framework, VS Code prompts you to install the framework package if it's not already present in the currently activated environment:
99
+
If you enable pytest, VS Code prompts you to install the framework package if it's not already present in the currently activated environment:
100
100
101
101

102
102
@@ -116,6 +116,7 @@ class Test_TestIncrementDecrement(unittest.TestCase):
116
116
deftest_increment(self):
117
117
self.assertEqual(inc_dec.increment(3), 4)
118
118
119
+
# This test is designed to fail for demonstration purposes.
119
120
deftest_decrement(self):
120
121
self.assertEqual(inc_dec.decrement(3), 4)
121
122
@@ -133,15 +134,16 @@ import inc_dec # The code to test
133
134
deftest_increment():
134
135
assert inc_dec.increment(3) ==4
135
136
137
+
# This test is designed to fail for demonstration purposes.
136
138
deftest_decrement():
137
139
assert inc_dec.decrement(3) ==4
138
140
```
139
141
140
142
## Test discovery
141
143
142
-
By default, the Python extension attempts to discover tests once you enable a framework. You can trigger test discovery at any time using the **Test: Refresh Tests** command.
144
+
By default, the Python extension attempts to discover tests once you enable a framework. You can also trigger test discovery at any time using the **Test: Refresh Tests** command from the command palette.
143
145
144
-
`python.testing.autoTestDiscoverOnSaveEnabled` is set to `true` by default, meaning that test discovery is also performed automatically whenever you add, delete, or update any Python file in the workspace. To disable this feature, set the value to `false`. You will need to reload the window for this setting to take effect.
146
+
`python.testing.autoTestDiscoverOnSaveEnabled` is set to `true` by default, meaning that test discovery is also performed automatically whenever you add, delete, or update any Python file in the workspace. To disable this feature, set the value to `false` which can be done either in the settings editors or in the `settings.json` file as described [here](https://code.visualstudio.com/docs/getstarted/settings). You will need to reload the window for this setting to take effect.
145
147
146
148
Test discovery applies the discovery patterns for the current framework (which can be customized using the [Test configuration settings](#test-configuration-settings)). The default behavior is as follows:
147
149
@@ -188,7 +190,7 @@ You can run tests using any of the following actions:
188
190
189
191
After a test run, VS Code displays results directly in the editor as gutter decorations. Failed tests will also be highlighted in the editor, with a Peek View that displays the test run error message and a history of all of the tests' runs. You can press `kbstyle(Escape)` to dismiss the view, and you can disable it by opening the User settings (**Preferences: Open Settings (UI)** command in the **Command Palette**) and changing the value of the **Testing: Automatically Open Peek View** setting to `never`.
190
192
191
-
In the **Test Explorer**, results are shown for individual tests and any classes and files containing those tests.
193
+
In the **Test Explorer**, results are shown for individual tests and any classes and files containing those tests. Folders will display a failure icon if any of the tests within that folder did not pass.
192
194
193
195

194
196
@@ -231,7 +233,7 @@ Support for running tests in parallel with pytest is available through the `pyte
231
233
232
234
## Debug tests
233
235
234
-
You might occasionally need to step through and analyze tests in the debugger, either because the tests themselves have a code defect you need to track down or in order to better understand why an area of code being tested is failing.
236
+
You might occasionally need to step through and analyze tests in the debugger, either because the tests themselves have a code defect you need to track down or in order to better understand why an area of code being tested is failing. For more information on debugging or to understand how it works in VS Code see [Python debugging configurations](/docs/python/debugging.md) and the general VS Code [Debugging](/docs/editor/debugging.md) article.
235
237
236
238
For example, the `test_decrement` functions given earlier are failing because the assertion itself is faulty. The following steps demonstrate how to analyze the test:
237
239
@@ -283,11 +285,11 @@ For example, the configuration below in the `launch.json` file disables the `jus
283
285
284
286
If you have more than one configuration entry with`"purpose": ["debug-test"]`, the first definition will be used since we currently don't support multiple definitions for this request type.
285
287
286
-
For more information on debugging, see [Python debugging configurations](/docs/python/debugging.md) and the general VS Code [Debugging](/docs/editor/debugging.md) article.
287
288
288
289
## Test commands
289
290
290
-
Below are all the supported commands for testing with the Python extension inVS Code:
291
+
Below are all the supported commandsfor testing with the Python extension inVS Code. These are all found via the command palette:
292
+
291
293
| Command Name | Description |
292
294
|------------|------------|
293
295
|**Python: Configure Tests**| Configure the test framework to be used with the Python extension. |
0 commit comments