Visual Studio Code for Python Developers Transcripts
Chapter: Testing your Code
Lecture: Enabling Test Support

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now that we understand how our project configuration is set up, let's actually run some tests. What I'm going to do is open up the command line,
0:09 and through that built-in terminal inside of VS Code, I'm just going to go ahead and run pytest. Now, because we had it installed as a dependency,
0:16 I should be able to just run pytest as a command in my command line inside of my virtual environment. If we expand the terminal a little bit,
0:24 you can see that it's picking up the pyproject.toml file as its configuration file. It's using that test folder as
0:31 the place that's going to look for the discover tests, and it's also picking up that pytest mock extension that we installed also.
0:38 We can be confident that our configuration is working so far. But what I actually want to do is run these tests using Visual Studio Code.
0:45 Now, on the left side in the activity bar, we should see a section here called Testing. Because we're inside of a Python project,
0:51 it's giving us an option to configure Python tests. Now, if we look at the top, it gives us the option to use two of
0:58 the most common Python testing frameworks, unit tests, which is built-in and it comes with the standard version of Python, and also pytest.
1:06 Now, we're going to use pytest for this course. Now, the next thing it's asking us is, well, what's the directory that contains the test?
1:13 Obviously, I'm going to select the test folder. Now, inside of this test explorer window on the left side, you can see that pytest has discovered
1:20 the test files and also the test inside of each of those files as well. If I hit ""Command comma"" or if I just go into the settings file, let's close
1:28 some of these windows really quickly, and I type Python testing, you should see some of the other options you can set as well,
1:35 such as the debug port or whether pytest is enabled or not, we can change it here, the binary path for pytest, or we can even toggle it and maybe we
1:43 changed our mind and we wanted to use unit tests instead. We can come inside of the configuration file, or we can change all of these
1:49 inside of our Visual Studio Code settings. Now, here's one more thing I want to show you that I particularly think is interesting.
1:56 Now, whenever we run our tests, you may or may not notice that on the left side, inside of the explorer,
2:02 there's a folder that got added called.pytestcache. That's just a caching file to help pytest run a little bit more efficiently.
2:09 But it also is a little bit distracting and I don't want to see inside of my code. What I'm going to do is I'm going to head back over to
2:16 the settings and I'm going to type in exclude. Now, I can start excluding file patterns. For instance, let's say I don't want to see
2:25 that inside of my folder view because it's not really important to me and I'm not going to really do anything with it.
2:29 I can just come in and I can do something like this. I'll use this catch-all pattern and then I'm going to do exclude.pytest_cache.
2:41 We'll click ""Okay"". Then notice now on the left side, it's completely gone. Anywhere that that file pops up inside of my project,
2:49 Visual Studio Code is just not going to show it to me. Obviously, if you wanted to see it again,
2:53 you can just come and hit this ""X"" here and it'll just pop back up. You can do this with any folder or
2:58 file that you want to hide from your Explorer view.


Talk Python's Mastodon Michael Kennedy's Mastodon