Link Search Menu Expand Document

Run, Debug, and Test

Visual Studio Code provides a set of toolkits and a strong interface for running, debugging, and testing your codes. In this section, we will learn the basics of running a Python code with VS Code and using frameworks and extensions to debug and test the code. The concepts presented here are not limited to VS Code and Python programming language.

Install support for your favourite programming language

Visual Studio Code supports almost every major programming language. While Web development languages, such as JavaScript and HTML, are supported right out of the box, you need to install extensions for other languages.

In this section, we learn how to run and debug a simple Python code with Visual Studio Code. As the first step, you need to install VS Code and Python 3 interpreter. If you are using Linux and you regularly update your machine, Python 3 is already installed. If you are using Windows 10, the easiest way is to enter python in the terminal. You can download and install Python from the Windows store. For other versions of Microsoft Windows, find the installation file from python.org and if you are using macOS, use Homebrew to install Python:

$ brew install python3`

You can verify the Python installation on Linux/macOS by running the following command in the terminal:

$ python3 --version

On Windows, open a command prompt and run the following command:

$ py -3 --version

Now, build a new directory and open Visual Studio Code. In Powershell or Bash, you can do that by entering the following commands:

$ mkdir hello
$ cd hello
$ code .

code opens VS Code and . tells the terminal to open VS Code in the current directory.

To run Python code in Visual Studio Code, you must tell it which interpreter to use. Open Common Palette and type Python: Select Interpreter command to search, then select the command. You can also use the Select Python Environment option on the Status Bar. VS Code may already have a selected interpreter and show it in the Status Bar.

From the File Explorer toolbar, select the New File button or Right Click > New File on your new directory. Name the file hello.py and type the following code:

msg = "Hello World"
print(msg)

Visual Studio code uses .py file extension to evaluate the contents with the selected interpreter.

To run your code with Python, click the Run Python File in Terminal play button on the top-right side of the editor. The button opens a terminal panel and runs the proper script in the shell.

Run and debug

VS Code has built-in debugging support for languages that get transpiled to JavaScript. For other languages, there are Debugger extensions in the VS Code Marketplace.

Select the Run icon in the Activity Bar on the side of VS Code to bring up the Run view. This tab displays all information related to running and debugging. You can insert Breakpoints or Logpoints and Run and Debug the code by pressing F5 or Run and Debug. A lunch configuration file is beneficial to configure and save debugging setup details. You can also inspect variables or modify their values.

To run the debugger, first, set a breakpoint on line 2 of hello.py by placing the cursor on the print call and pressing F9. Now, press F5 to initialize the debugger. VS Code opens a configuration menu the first time you debug a file. Choose Python File, which is the configuration that runs the current file shown in the editor using the currently selected Python interpreter.

The debugger will stop at the first line of the file breakpoint. The current line is indicated with a yellow arrow. If you examine the Local variables window, by clicking Run and Debug in the Activity Bar, you will see now defined msg variable appears in the Local pane.

Writing proper comments in your code helps other developers to easily understand your code and add new functionality. Python Docstring Generator automates the process of creating a docstring for the functions you define in Python.

Testing

Unit testing is a software testing method in which you test specific pieces of code, also known as units, automatically and with a full range of different inputs, including boundary and edge cases. Unit tests are concerned only with the inputs and outputs of a unit, not with its implementation. To thoroughly test a function, you want to throw every conceivable input at it. For each input, you define the function’s expected return value.

To run all unit tests automatically, you write the tests themselves, which are pieces of code that call the function with a particular input and compares the actual return value with the expected return value. The test report tells you whether the unit is behaving as expected across all test cases. When a unit passes all of its tests, you can be confident that it is functioning properly.

Example: Python test framework with Python Extension

The Python Extension adds the testing capability to VS Code, but it is disabled by default. You can enable it by selecting Python: Configure Tests in the Command Palette. This command prompts you to select a test framework, the folder containing tests, and the pattern used to identify test files. After enabling it, VS Code prompts you to install pytest, which is one of the common testing platforms for Python.

You can make a folder in the root folder of your project and name it pytest. Create a file named func_pytest.py in this folder. This file contains all the tests we would like to write for the unit.

Main Function:

def add(x, y):
    return x + y

Unit test:

def test_add():
    assert add(1, 2) == 3

Now if you open the TEST bar on the left side, you can see all the tests you have defined in this file and you can either run them one by one or all at once.

Now, write a function to subtract two numbers and write a test for that function.