Modern Python Projects Transcripts
Chapter: Your code editor
Lecture: Testing Python code

Login or purchase this course to watch this video and the rest of the course contents.
0:00 VSCode, also has a nice side menu for running your tests, but by default, it's not visible.
0:06 So, to enable it, we have to open the command palette and run Python configure
0:13 tests. From this list, we have to select which testing framework we're going to
0:17 use. In this course, I will be using pytest because it's much better than the building unit tests. So, let's go ahead and select pytest right now.
0:25 And now we have to select the directory that contains your tests. Normally, you would store them in a folder called Tests or something like that.
0:33 But here, I only have one test file that it's in the root folder. So, let's go ahead and select root. If you don't have pytest installed,
0:42 you might get a pop up saying that you should install pytest. I went ahead and I installed pytest using pip. As you can see, it's already installed.
0:54 If you already had pytest installed and you had no warning, or maybe if you selected a different testing framework,
1:00 you should now see that we have a different icon on the sidebar. This is the testing menu. We only have one test file,
1:06 and there is this great question mark next to it because those tests have not been run yet. So test around them.
1:15 And as you can see, all three tests are passing, and we also have the check mark above each of them.
1:22 That's a very optimistic scenario, but usually you will have some tests that are failing So let's try to modify one of our tests and see what happens.
1:33 As you can see when one of the tests is failing, you have an indication right above the test. But also in the sidebar. We can now,
1:41 for example, debug this test to see what's going on. If we click here, we get the output from the debugging, but we can actually do a bit more.
1:51 For example, we can put break points in our tests. Let's copy this line so the break point doesn't exit because that was the last line
1:58 in our file. And let's put a break point here and let's rerun Let's Save It first. And now let's rerun it.
2:11 So now we have this debug toolbar on the top, and if we go to the debug side panel,
2:17 you can see that we have the same debug information and as we had in the previous lesson, so we can investigate local variables,
2:24 we can watch some of the variables. We still have the surname from the previous lesson,
2:28 so let's remove it. So let's stop debugging session and let's go and fix our
2:35 test. You can see it's still greyed out, because we haven't run this test after we modified it. So, let's run it.
2:47 And as you can see, our tests are green again. There are a few other useful tools in the sidebar, so you can run all the tests. We can debug them,
2:54 you can discover test. So, for example, if we create a new file, VSCode didn't detect this test file. So, if we want to add it to the sidebar,
3:11 we just have to run discover test again. And now we have our new file. So, let's rerun all the tests and all of them are passing.
3:21 I will talk more about pytest later in this course, we're gonna have a chapter related to pytest and testing in general.
3:28 But right now I can already tell you that pytest accepts a configuration file called pytest.init
3:33 I'm telling you this right now because if you create a pytest.init VSCode will actually respect options from that file. So, let's give it a try.
3:43 Let's go to our folder. Let’s add the new file, and let's add the configuration option here. So, first we need to write pytest.
3:54 For example, we can add an option that will make pytest fail after the first failed tests. So, we don't want to run all the tests if we know
4:02 that one of them failed. Let's make it stop after the first failure. We can achieve this by adding --maxfail=1. to the adopts parameter.
4:13 Let's go back to our testing tab and let's rerun all the tests. Since they're passing, nothing has changed. So, let's change some things here.
4:23 Let's change all those tests to make them fail, As you can see, After the first Test failed, the other two didn't run. So, when we fix it,
4:40 we can rerun it, now this one passed, this one failed, and since this one failed, the last one was not rerun.
4:48 So, that's how you can use the testing menu in VSCode

Talk Python's Mastodon Michael Kennedy's Mastodon