Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 45 additions & 44 deletions docs/python/debugging-symbols-for-mixed-mode-c-cpp-python.md

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions docs/python/includes/project-from-existing.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
---
ms.topic: include
---

1. Launch Visual Studio and select **File** > **New** > **Project**.

1. In the **New Project** dialog, search for "Python", select the **From Existing Python code** template, give the project a name and location, and select **OK**.

1. In the wizard that appears, set the path to your existing code, set a filter for file types, and specify any search paths that your project requires, then select **Next**. If you don't know what search paths are, leave that field blank.

:::image type="content" source="../media/projects-from-existing-1.png" alt-text="Screenshot of a New Project creation from Existing Code window, step 1.":::
:::image type="content" source="../media/projects-from-existing-1.png" alt-text="Screenshot of a New Project creation from Existing Code window, step 1.":::

1. In the next dialog, select the startup file for your project and select **Next**. If necessary, select an environment; otherwise accept the defaults.

> [!Note]
> The dialog shows only files in the root folder; if the file you want is in a subfolder, leave the startup file blank and set it later in **Solution Explorer** (described next).
> [!Note]
> The dialog shows only files in the root folder; if the file you want is in a subfolder, leave the startup file blank and set it later in **Solution Explorer** (described next).

:::image type="content" source="../media/projects-from-existing-2.png" alt-text="Screenshot of New Project Creation from Existing Code window, step 2.":::
:::image type="content" source="../media/projects-from-existing-2.png" alt-text="Screenshot of New Project Creation from Existing Code window, step 2.":::

1. Select the location in which to save the project file (which is a `.pyproj` file on disk). If applicable, you can also include autodetection of virtual environments and customize the project for different web frameworks. If you're unsure of these options, leave them set to the defaults.
1. Select the location in which to save the project file (which is a `.pyproj` file on disk). If applicable, you can also include auto-detection of virtual environments and customize the project for different web frameworks. If you're unsure of these options, leave them set to the defaults.

:::image type="content" source="../media/projects-from-existing-3.png" alt-text="Screenshot of a New Project creation from Existing Code window, step 3.":::
:::image type="content" source="../media/projects-from-existing-3.png" alt-text="Screenshot of a New Project creation from Existing Code window, step 3.":::

1. Select **Finish** and Visual Studio creates the project and opens it in **Solution Explorer**. If you want to move the `.pyproj` file elsewhere, select it in **Solution Explorer** and choose **File** > **Save As**. This action updates file references in the project but doesn't move any code files.

Expand Down
82 changes: 42 additions & 40 deletions docs/python/includes/vs-2019/unit-testing-python.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Unit test Python code
description: Setting up unit testing for Python code in Visual Studio takes full advantage of Test Explorer features to discover, run, and debug tests.
ms.date: 04/01/2022
ms.date: 08/18/2023
ms.topic: how-to
author: cwebster-99
ms.author: cowebster
Expand All @@ -15,16 +15,18 @@ ms.workload:

## Select the test framework for a Python project

Visual Studio supports two testing frameworks for Python, [unittest](https://docs.python.org/3/library/unittest.html) and [pytest](https://pytest.org/en/latest/) (available in Visual Studio 2019 starting with version 16.3). By default, no framework is selected when you create a Python project. To specify a framework, right-click on the project name in Solution Explorer and select the **Properties** option. This opens the project designer, which allows you to configure tests through the **Test** tab. From this tab, you can select the test framework that you want to use for your project.
Visual Studio supports two testing frameworks for Python, [unittest](https://docs.python.org/3/library/unittest.html) and [pytest](https://pytest.org/en/latest/) (available in Visual Studio 2019 starting with version 16.3). By default, no framework is selected when you create a Python project. To specify a framework, right-click on the project name in Solution Explorer and select the **Properties** option. This opens the project designer, which allows you to configure tests through the **Test** tab. From this tab, you can select the test framework that you want to use for your project.

* For the **unittest** framework, the project's root directory is used for test discovery. This location, as well as the text pattern for identifying tests, can be modified on the **Test** tab to user specified values.
* For the **pytest** framework, testing options such as test location and filename patterns are specified using the standard pytest .ini configuration file. See the [pytest reference documentation](https://docs.pytest.org/en/latest/reference.html#ini-options-ref) for more details.
- For the **unittest** framework, the project's root directory is used for test discovery. This location, as well as the text pattern for identifying tests, can be modified on the **Test** tab to user specified values.
- For the **pytest** framework, testing options such as test location and filename patterns are specified using the standard pytest .ini configuration file. See the [pytest reference documentation](https://docs.pytest.org/en/latest/reference.html#ini-options-ref) for more details.

Once you've saved your framework selection and settings, test discovery is initiated in the Test Explorer. If the Test Explorer window is not already open, navigate to the toolbar and select **Test** > **Test Explorer**.

## Configure testing for Python without a project
Visual Studio allows you to run and test existing Python code without a project, by [opening a folder](../../quickstart-05-python-visual-studio-open-folder.md) with Python code. Under these circumstances, you'll need to use a **PythonSettings.json** file to configure testing.
1. Open your existing Python code using the **Open a Local Folder** option.

Visual Studio allows you to run and test existing Python code without a project, by [opening a folder](../../quickstart-05-python-visual-studio-open-folder.md) with Python code. Under these circumstances, you'll need to use a **PythonSettings.json** file to configure testing.

1. Open your existing Python code using the **Open a Local Folder** option.

![The Visual Studio startup screen](../../media/quickstart-open-folder/01-open-local-folder.png)

Expand All @@ -33,32 +35,31 @@ Visual Studio allows you to run and test existing Python code without a project,
![Show all files button](../../media/unit-test-show-files.png)

1. Navigate to the **PythonSettings.json** file within the **Local Settings** folder. If you don't see this file in the **Local Settings** folder, create it manually.

1. Add the field **TestFramework** to the settings file and set it to **pytest** or **unittest** depending on the testing framework you want to use.

```json
{
"TestFramework": "unittest",
"UnitTestRootDirectory": "testing",
"UnitTestPattern": "test_*.py"
}
```
```json
{
"TestFramework": "unittest",
"UnitTestRootDirectory": "testing",
"UnitTestPattern": "test_*.py"
}
```

> [!Note]
> For the **unittest** framework, if the fields **UnitTestRootDirectory** and **UnitTestPattern** are not specified in the PythonSettings.json file, they are added and assigned default values of "." and "test*.py" respectively.
> [!Note]
> For the **unittest** framework, if the fields **UnitTestRootDirectory** and **UnitTestPattern** are not specified in the PythonSettings.json file, they are added and assigned default values of "." and "test\*.py" respectively.

1. If your folder contains a **src** directory that is separate from the folder that contains your tests, specify the path to the **src** folder using the **SearchPaths** field in your **PythonSettings.json** file.

```json
{
"TestFramework": "unittest",
"UnitTestRootDirectory": "testing",
"UnitTestPattern": "test_*.py",
"SearchPaths": [ ".\\src"]
}
```
```json
{
"TestFramework": "unittest",
"UnitTestRootDirectory": "testing",
"UnitTestPattern": "test_*.py",
"SearchPaths": [".\\src"]
}
```

1. Save your changes to the PythonSettings.json file to initiate test discovery for the specified framework.
1. Save your changes to the PythonSettings.json file to initiate test discovery for the specified framework.
> [!Note]
> If the Test Explorer window is already open **CTRL** + **R,A** also triggers discovery.

Expand All @@ -69,33 +70,34 @@ By default, Visual Studio identifies **unittest** and **pytest** tests as method
1. Open a [Python project](../../managing-python-projects-in-visual-studio.md).

1. Once the project is loaded in Visual Studio, right-click your project in Solution Explorer and select the **unittest** or **pytest** framework from the Properties **Test** tab.

> [!Note]
> If you use the pytest framework, you can specify test location and filename patterns using the standard pytest .ini configuration file. By default, the workspace/project folder is used, with a pattern of `test_*py` and `*_test.py`. See the [pytest reference documentation](https://docs.pytest.org/en/latest/reference.html#ini-options-ref) for more details.

1. After the framework is selected, right-click the project again and select **Add** > **New Item**, then select **Python Unit Test** followed by **Add**.

1. This action creates a *test_1.py* file with code that imports the standard `unittest` module, derives a test class from `unittest.TestCase`, and invokes `unittest.main()` if you run the script directly:
1. This action creates a _test_1.py_ file with code that imports the standard `unittest` module, derives a test class from `unittest.TestCase`, and invokes `unittest.main()` if you run the script directly:

```python
import unittest
```python
import unittest

class Test_test1(unittest.TestCase):
def test_A(self):
self.fail("Not implemented")
class Test_test1(unittest.TestCase):
def test_A(self):
self.fail("Not implemented")

if __name__ == '__main__':
unittest.main()
```
if __name__ == '__main__':
unittest.main()
```

1. Save the file if necessary, then open **Test Explorer** with the **Test** > **Test Explorer** menu command.

1. **Test Explorer** searches your project for tests and displays them as shown below. Double-clicking a test opens its source file.

![Test Explorer showing default test_A](../../media/unit-test-a-2.png)
![Test Explorer showing default test_A](../../media/unit-test-a-2.png)

1. As you add more tests to your project, you can organize the view in **Test Explorer** using the **Group By** menu on the toolbar:

![Tests Explorer Group By toolbar menu](../../media/unit-test-group-menu-2.png)
![Tests Explorer Group By toolbar menu](../../media/unit-test-group-menu-2.png)

1. You can also enter text in the **Search** field to filter tests by name.

Expand All @@ -113,20 +115,20 @@ Tests run in the background and **Test Explorer** updates each test's status as

- Passing tests show a green tick and the time taken to run the test:

![test_A passed status](../../media/unit-test-A-pass.png)
![test_A passed status](../../media/unit-test-A-pass.png)

- Failed tests show a red cross with an **Output** link that shows console output and `unittest` output from the test run:

![test_A failed status](../../media/unit-test-A-fail.png)
![test_A failed status](../../media/unit-test-A-fail.png)

![test_A failed with reason](../../media/unit-test-A-fail-reason.png)
![test_A failed with reason](../../media/unit-test-A-fail-reason.png)

## Debug tests

Because unit tests are pieces of code, they are subject to bugs just like any other code and occasionally need to be run in a debugger. In the debugger you can set breakpoints, examine variables, and step through code. Visual Studio also provides diagnostic tools for unit tests.

> [!Note]
> By default, test debugging uses the ptvsd 4 debugger for Visual Studio 2017 (versions 15.8 and later) and debugpy for Visual Studio 2019 (versions 16.5 and later). If you would like to instead use ptvsd 3, you can select the **Use Legacy Debugger** option on **Tools** > **Options** > **Python** > **Debugging**.
> By default, test debugging uses the ptvsd 4 debugger for Visual Studio 2017 (versions 15.8 and later) and debugpy for Visual Studio 2019 (versions 16.5 and later). If you would like to instead use ptvsd 3, you can select the **Use Legacy Debugger** option on **Tools** > **Options** > **Python** > **Debugging**.

To start debugging, set an initial breakpoint in your code, then right-click the test (or a selection) in **Test Explorer** and select **Debug Selected Tests**. Visual Studio starts the Python debugger as it would for application code.

Expand Down
Loading