Python Unit Testing With VS Code
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: https://github.com/JFincher42/PyEval.git
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 expr_test.py
. 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.
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.
haleybear on April 24, 2019
link to repo: github.com/JFincher42/PyEval.git
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:
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.
I tried running git command and am getting git is invalid identifier
danP on Nov. 1, 2021
Currently using VS Code 1.61.2, and the interface seems to have changed rather significantly.
The full testing interface can be reached from the “beaker” in the left navigation (code.visualstudio.com/docs/python/testing#test-discovery for screenshots), and I have no “run tests” option in the bottom status bar.
Also, in the command palette, the commands for unittest begin with Test: (eg, Test: Run All Tests).
CG-Tespy on Nov. 14, 2022
Am running VS Code 1.73.1 at the moment, and I tried the Run All Tests command. I got a popup telling me that there’s no test framework configured. For those who don’t know what to do from this point on, here’s what I did:
- Click “Configure Test Framework” on the popup that said “No test framework configured”
- The Command Palette should then open. Choose the unittest Standard Python Test Framework option it gives you
- It’ll then ask you what directory contains the tests. Choose the root
- It’ll then ask you the pattern to identify test files. Choose (asterisk)(underscore)test.py
- Wait a few seconds
- Now you can run all tests
I hope this helps
Luke on March 25, 2023
CG-Tespy this helped a lot, thank you. I was very confused by the video, until I found your comment.
Ranga on April 5, 2023
@CG-Tepsy, that was very helpful indeed. Thank you.
Dakkon on April 24, 2023
Likewise - CG-Tespy, thanks for the tip! I was frustrated with the video until I read your comment, which was seriously helpful.
Morris Johnson on Dec. 20, 2023
Interesting to see how testing was made possible in the past. Unfortunately, there have been too many change in vs code since this video was made to make much use of it.
Become a Member to join the conversation.
Brendan Leber on April 5, 2019
FYI, if you are running the “Insider’s Edition” the shell command to run is “code-insiders .”