CHAPTER 6Unit Testing

Visual Studio Code and the Python extension provide a great interface for testing within the Test Explorer view. You can use Visual Studio Code to work with unit tests written in unittest, pytest, and nose. This chapter walks through unit testing for unittest and pytest with a simple example from the Python Koans repository.

The exercises within this chapter are completed within the Triangles folder. The Triangles folder contains two subfolders: unittest and pytest. Each subfolder contains code and a test file for the respective framework. Before proceeding with the exercises, open the subfolder for the preferred framework in Visual Studio Code and activate a virtual or conda environment for the workspace.

Enable and Discover Tests

Unit testing in the Python extension is disabled by default. You must enable a test framework to run unit tests, and only a single test framework can be enabled at a time. (To switch frameworks, disable the current framework and enable the new one.)

To enable a test framework, complete the following:

  1. Run the command Python: Configure Tests.
  2. Select the framework.
  3. Select the directory that contains the test.
  4. Select the pattern to identify test files.

Get Visual Studio Code for Python Programmers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.