{{announcement.body}}
{{announcement.title}}

How to Set Up Visual Studio Code for Python Testing and Development

DZone 's Guide to

How to Set Up Visual Studio Code for Python Testing and Development

VS Code is an open-source, light-weight IDE that is gaining popularity due to its flexibility and cross-platform support.

Free Resource

Image title

Learn more about using VS Code for Python testing and development

VS Code is an open-source, light-weight IDE that is gaining popularity due to its flexibility, ability to configure different programming languages, and cross-platform support. The following tutorial will walk you through how to set up a development and test environment in Python.

  • Download VS Code here
  • Install Python and configure your environment if you haven’t done so already
Interested in VS Code for Java development? Check out Visual Studio Code for Java: The Ultimate Guide 2019.

We will cover the following topics:

  • Configuring VS Code for Python
  • Configuring the debugger
  • Configuring Liniting: PEP8
  • Configuring Pytest

Let's get started!

Configuring VS Code for Python

Open the VS Code:

Opening VS code for Python

Install the Python extension from your extensions:

Python extensions for VS Code

Create a project workspace/folder. File -> Open Folder

Opening a folder in VS Code

Select a Python interpreter.

Open Command Palette (ctrl +shift +P) and start typing ‘python: select interpreter.’It will display a list of available Python environments.

VS Code command palette

Create a new file and IntelliSense. IntelliSense will work for standard modules and packages installed.

VS Code IntelliSense

When running a program, go to the Editor and right-click select ‘run python file in terminal.’ The output will be displayed in the terminal, as below:

VS Code Editor

Configuring Debugger

To initialize debug configurations, first select the Debug View in the sidebar:

If you don't yet have any configurations defined, you'll see No Configurations in the drop-down list, and a dot on the settings icon:

No Configurations in VS Code

From the settings, you can configure the debugger, selecting default debugger here.

Configuring the debugger in VS Code

Configuring Linter: PEP8

Open Command Palette (ctrl +shift +P) and start typing ‘python: select linter.’ It will display a list of available Python linters. You can add any of the settings to your user settings.json file (opened with the File > Preferences > Settings:

Configuring Linter PEP8 in VS Code

Configuring Tests: Pytest

The VS Code Python extension supports unit tests as well as pytest. Here's how to enable the framework:

Open Command Palette (ctrl +shift +P) and start typing ‘python: configure tests.’ It will display a list of available python linters. You can add any of the settings to your user settings.json file (opened with the File > Preferences > Settings

Configuring tests with Pytest in VS Code

After configuring the tests, it will display the following, opening the test will ask for the test location, in the following example selected the root folder.

If discovery succeeds, the status bar shows Run Tests instead:

Run tests in VS Code

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:

Test discovery failed in VS Code

Running tests can be done at test level or suite level, as shown in the below screen:

Running tests on VS Code

Test results can found here:

VS Code test results

Further Reading

Visual Studio Code for Java: The Ultimate Guide 2019

Top 5 Must-Have Visual Studio Code Extensions

Topics:
python ,vscode ,pytest ,open source ,testing ,python development ,visual studio code ,visual studio ,debugging

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}