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