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.
Join the DZone community and get the full member experience.
Join For FreeVS 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:
Install the Python extension from your extensions:
Create a project workspace/folder. File -> Open Folder
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.
Create a new file and IntelliSense. IntelliSense will work for standard modules and packages installed.
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:
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:
From the settings, you can configure the debugger, selecting default debugger here.
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 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
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:
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:
Running tests can be done at test level or suite level, as shown in the below screen:
Test results can found here:
Further Reading
Opinions expressed by DZone contributors are their own.
Comments