Visual Studio Code for Python Developers Transcripts
Chapter: Testing your Code
Lecture: Reviewing the Project Layout

Login or purchase this course to watch this video and the rest of the course contents.
0:00 >> If you take a look at the sample code for this chapter, you might notice that we've made a few changes to our project structure.
0:08 Now, even though these are small changes, they do have an impact on the way that we're going to test our code. First off, you might notice that there's
0:15 now a test folder in the root of the project. As you can imagine, this is where all of the unit testing code is going to go.
0:22 Secondly, there's no longer our requirements.txt file. Instead, we're going to specify our dependencies inside of the pyproject.toml file.
0:31 Also, you'll notice that we're going to have some of the other project configuration located in this file as well.
0:37 Why don't we head over to Visual Studio Code and I can show you how some of these changes impact our project. As I mentioned before,
0:45 we no longer have a requirements.txt file to specify our dependencies. Instead, we're going to use the pyproject.toml file. If I open up the file,
0:54 underneath that project section, you'll notice that I have a dependencies property. Inside of here, I could specify that I need FastAPI,
1:02 TinyDB, and any other dependency that I need for the production version that I'm going to run for my application.
1:09 Now, I say production version because remember, we also need to have dependencies for our tests,
1:15 but we might not want to have those dependencies installed whenever we run our project, so we want to keep them separate.
1:21 If you look down at the project optional dependency section, you'll notice that I have a test property. Inside of here, I can specify
1:29 the test dependencies that I want to run. If we take a look down at the project optional dependency section, you'll see I have a property called test.
1:36 Inside of here, I have an array of dependencies where I let the project know that I need pytest, and I also need a pytest plugin called pytestMock.
1:45 Notice how we have the option to specify conditions for the versions that we want for those particular packages.
1:51 Now, if I was still using a requirements.txt file, it'd be a little bit challenging because I may have to specify two different files
2:00 or do some type of special build configuration if I wanted to keep my production dependencies and my test dependencies separate.
2:07 Here inside the PyProject file, I could just have different sections that talk about my main dependencies,
2:14 and also I can specify optional dependencies as well. If we scroll down a little bit inside of the PyProject.toml file,
2:20 you'll notice that not only can I specify information about my project, like the dependencies and some metadata,
2:26 but I can also set up configuration for pytest too. Inside of the tool.pytest.ini_options section, I'm able to specify pytest configuration properties,
2:38 such as the Python path, test pass, and even different markers. I can even do the add-ops where I can specify command line options that I want to use.
2:47 Again, this would typically be in a separate file, but because we're using PyProject.toml, I'm able to consolidate my configuration and not have
2:55 so many different files to maintain. Now, if I was supposed to open this test folder, you can see some of the tests that
3:01 I've already written for our project. Now, notice how I'm already getting red squiggly lines. Well, that's because I haven't installed
3:08 any of my dependencies yet inside of my project. What I'll do is I'll open up the command line,
3:15 and let's go ahead and install some of these dependencies. I'm going to do Python-M, I'm going to do install. Now, the dot is what we use when we're
3:26 going to use our PyProject.toml file. I'm going to go ahead and install this, and in a few seconds, you should see that it's going to install
3:34 the dependencies that we have set up inside of our PyProject.toml. Now, if I do something like pip list,
3:42 I should see everything that just got installed. But what I don't see is pytest. Well, pytest didn't get installed because if you recall,
3:51 let's head back over to this PyProject.toml file a little bit, let's move this window down a little bit so you can see what's going on.
3:57 I specify this in optional dependencies. If I just do install, it's not going to install those optional dependencies.
4:03 I have to let it know that I wanted to do that. I'm going to do Python-M pip install, but I'm also going to specify
4:11 some of those optional dependencies. The only optional dependency section or property I have here is test.
4:18 What you'll see inside of my command line now, it's going to install pytest and pytestMock using those version specifiers that I set up.
4:26 Now, if I go ahead and I do pip list, I should see I do have all those other dependencies, but I also have pytest and I have pytestMock.
4:36 Using PyProject.toml just gives me a really convenient way for me to manage different dependencies for
4:42 the different types of environments I want to use. Here I have my core dependencies and I also have my test dependencies specified as well.


Talk Python's Mastodon Michael Kennedy's Mastodon