Visual Studio Code for Python Developers Transcripts
Chapter: Testing your Code
Lecture: Debugging Tests
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
With everything that we've covered so far in this chapter, we should have most of what we need to start running
0:06
a debugging test inside of Visual Studio Code. Now, I'm going to open up the Test Explorer. Now, you can see that we actually
0:13
haven't run any of the tests yet, so there's no visual indicator to let us know whether they're successful or whether they're failed or not.
0:19
If you look at the top menu, we can see that we have a couple of options that we can run here. We could refresh the test if we wanted to.
0:27
Maybe you've written some new tests and you want to force the editor to run its discovery. You can use this button for that.
0:33
We can run all the tests and then there's also a debug one that we can debug all our tests as well. In this tree view underneath,
0:39
we can see that our tests are grouped by files and folders. All our tests are inside of this test folder,
0:46
and then we have two test files or two test modules. One for testing the categories router and another one for testing the status router.
0:54
If I open up this tree, you'll see all the tests listed that I have for that category router. Now, if we wanted to go ahead and run all the tests,
1:01
I'll just go ahead and hit this ""Run all tests"" button. You'll see some similar output to what we saw
1:06
in the command line when we ran pytest directly. If I give us a little bit of space here, you can see we have a test results tab,
1:12
and then we can scroll through and we can see that all of our tests have been passing so far. On the right side of the test results window,
1:19
we can see that we've also captured some data about that latest test run. Now, it's no fun when all your tests pass.
1:27
Let's go ahead and shake some stuff up. Let's go ahead and break one of these tests. What I'm going to do is I'm going to click on
1:33
the ""Test"" and then right next to it, I can click ""Go to test"". What it'll do is it'll send me right over to where that file is or where
1:40
that test is defined inside of the editor. Now, I'm going to do something like this. I'm just going to mess this up,
1:45
and now this is not going to work anymore. Now, if I go ahead and run all tests, you should see that this test should fail.
1:53
Oh boy. No one wants to see that. Let's go ahead and fix this super quick. Now, instead of me running all the tests all the time,
2:03
maybe I just want to fix this one particular test, and not everything inside of my test suite.
2:08
I'm going to close this error window here that's telling me everything that went wrong and what problems I do have on my test.
2:15
I'm going to go ahead and fix that error really quickly. But now that it's fixed, notice that this little X that's right next to it.
2:22
Well, this let me know that the test failed previously. But if I click it again, it'll rerun the test. It'll only rerun that single test,
2:30
and it'll let me know what happened. At the bottom, you can see I have one test passed in my test results window, and it also logged it here,
2:37
my test runs at the right side. Now, what if I wanted to go a little bit deeper? What if I actually wanted to debug a test?
2:42
Well, we did cover running and debugging Python applications in a previous module, but running tests is so much easier.
2:49
All we literally have to do is just hit the ""Debug"" button. So I'm going to select the test inside of
2:54
the Explorer and I'm going to go ahead and run it. Now Visual Studio Code has launched debugger, it's running my test, and now I could step
3:02
through using all my usual debug functions like step over, step into, continue, stop, and all these other types of things. I have my variables window,
3:13
my watch window, my call stack. Everything that I would expect from a typical Python debugging session is available to me.
3:19
I also just noticed that I have a typo here, so let's go ahead and fix that real quick. There we go, that's better. Let's make sure inside of VS Code
3:26
that the name is also updated as well. Notice how it looks at it like, well, it's a new test because I renamed it, I guess.
3:34
So let's go ahead and run this test, make sure it still works, and everything here looks pretty good. Now, another thing you can do inside of
3:42
this test Explorer window if you're interested, is you can actually run a different types of filters. So you could say things like,
3:48
show me only the test that fails, show me some of the excluded tests, maybe use skip tests or something like that.
3:55
Now, you know how to use the test Explorer to run and debug your tests written in Python.