Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Python Unit Testing With VS Code

Give Feedback

VS Code can automatically recognize existing Python tests and run them. Learn how to configure it to run test suites, individual tests as well as view test output.

Link to PyEval repo:

00:00 Just like the full-fledged version of Visual Studio, VS Code allows for easy unit testing, which allows us to test specific parts of our code one at a time. To demonstrate this I’m going to use PyEval, a Python library for parsing equations written in infix notation.

00:20 This was written by Jon Fincher, the original author of the article that this video series is based on. He used to work at Microsoft, so you know that this material is good stuff. To start, we’re going to open the Command Palette by hitting Command + Shift + P, or Control + Shift + P on other systems.

00:39 Now we’ll type shell and select the command that says Install ‘code’ command in PATH. What this will do is add Visual Studio Code to your system path, which dictates what folders your terminal searches for when it tries to run a program that you specify.

00:58 Now I’ll clone Jon’s repository to my local machine, I’ll open a new system terminal here—there we go. And I’ll change directory to the desktop by typing cd Desktop/.

01:12 Now we’ll type git clone and paste in the clone URL. This link will be available in the description down below the video. We’ll press Enter and let Git clone the remote repository to my machine.

01:28 Now that it’s done, we can move into the cloned repository by typing cd PyEval/. To open this folder within Visual Studio Code, we can type code dot (.).

01:41 Here, code is the name of the binary that launches VS Code, and this is works because we added VS Code to the system path. The . represents the current directory, so we’re basically telling VS Code to open the current directory when we press Enter. And there we go! On the left, you’ll see all of the files in this project.

02:03 Let’s click on This file contains all of the unit tests that can be run. Right now, VS Code is complaining at me because this project came with a settings.json file specifying where the Python path is, as well as some other files for Python.

02:23 Your mileage may vary with this if you’re using a virtual environment, but right now I’m not using one. So here’s how we can fix this. First, I’ll click on this button here to bring up this familiar menu, which lets us change our Python interpreter.

02:40 I’ll select the latest version installed on my system. But now it’s still complaining to me that it can’t find unittest. To make sure that our change has actually been applied, we can head over to the settings.json file under the .vscode/ directory.

02:58 We’ll make that the Python path here is set correctly. This right here should point to the Python executable on your system. The settings file downloaded from the repository sets this to "/usr/local/bin/", which is where Python is installed on Linux. I’m using macOS right now, so selecting the Python interpreter a moment ago updated this file to reflect the correct location of my Python interpreter.

03:25 Once you’re positive that this is correct for your system, we’ll go ahead and restart VS Code.

03:36 Now, if we go back into our expr_test file, all of the errors should be gone. Again, this may not be an issue for you at all, depending on your configuration and how you’re running your program.

03:50 I just showed you an easy way to fix any settings that might come with a Git repository that don’t quite line up with the system settings you need. Now we can finally run our unit tests. In the status bar at the bottom here, you’ll notice a little lightning bolt that says Run Tests. Click on that, and then at the very top, choose Run All Unit Tests.

04:14 After a few seconds, you should see a green check mark. This is telling us that all of our tests passed. To actually view test results, click on the check mark and choose View Unit Test Output at the top.

04:28 This will show us a log of each test that ran. If you don’t see any output here, make sure that the OUTPUT panel is set to Python Test Log.

04:39 You can also run individual tests by clicking on the check mark at the bottom and then choosing Run Unit Test Method…. And now we can pick a single test to run.

04:51 I’ll choose this one right here. And now we’ve run just one test, as you can see in the output panel below. This way, if you just want to run a specific test, you don’t have to wait for all of the other tests to finish first.

05:05 Alternatively, you can also find the class or test method in your code and then click on Run Test above the definition. In the next video, we’ll learn how to debug our Python programs.

Brendan Leber on April 5, 2019

FYI, if you are running the “Insider’s Edition” the shell command to run is “code-insiders .”

Austin Cepalia RP Team on April 5, 2019

Great tip! I’ll add it to the video descriptions I’m writing now

AugustoVal on April 6, 2019

Hello Austin,

Where could I find the link for the repo?

Jacob Andersen on April 7, 2019

I’m using visual studio code for windows and can’t seem to find the shell command shown 43 seconds into the video. I open the command palette just fine, but there is no sign of the ‘install code command in PATH ‘ statement.

Austin Cepalia RP Team on April 7, 2019

It appears that the VS Code installer for Windows automatically adds the program executable to the PATH, so that step can be ignored. You should still be able to type “code .” in your command prompt or bash shell, just as I did in the video.

Anonymous on July 24, 2019

I have no shell command ” Install ‘code’ Command in path”

Anonymous on July 24, 2019

Code is not automatically installed in path on my windows machine.

nskgithub on Nov. 26, 2019

I have the same issue as below:

Im using visual studio code for windows and cant seem to find the shell command shown 43 seconds into the video. I open the command palette just fine, but there is no sign of the install code command in PATH  statement.

I tried running git command and am getting git is invalid identifier

Become a Member to join the conversation.