Modern Python Projects Transcripts
Chapter: Testing code
Lecture: Running pytest tests
0:00 So we have our pytest example file with some tests here. Now, we actually have to run it somehow.
0:07 One way we could do this would be to configure the VSCode to use pytest and then running from VSCode. But probably most of the time,
0:14 you will be running your test from the terminal. So, let's do this. When you run, pytest command in your terminal.
0:24 pytest will look inside the current directory or it's subdirectories for files that either start or end, with test and inside of those files,
0:34 it will select all functions that starts with test either outside of any class or inside of a class prefix with test again.
0:41 All this is called test Discovery. And by following those simple conventions, you can make sure that pytest will work out of the box.
0:50 So as you can see, two interesting thing happened. First of all, pytest didn't run our pytest example file because the name doesn't
0:58 start with test, so we have to either re name this file or point pytest
1:02 directly to it. But another interesting thing is that pytest had rerun our unit tests
1:08 and it actually managed to run all those tests and make sure they are passing.
1:12 So what's really cool about pytest is that it can also run tests written in the unit test. If you have a large code base of tests written in unit
1:21 test and you want to move to pytest, you will be able to check both test written in unit test and the new tests
1:28 that you write in pytest. So migration from unit test to pytest can be very easy because you still keep your old test and the new
1:35 tests are written in pytest framework. Let's go back to actually running test from our pytest example.
1:43 The easiest way is to just write pytest and point to the file. And now pytest had run all three tests from our file, and all of them are passing.
1:55 If something was wrong with this file, let's say this should return false.
2:02 We would get such an error message, out of the box is not super helpful because it says, assert false is true,
2:10 but assert accept additional parameter, and this is the message that will be printed. If the assertion is false, so we can say something like that,
2:25 and here we have it, foo is not upper. You can also specify a specific class or even a specific function from this file that
2:33 you want to run. So let's say we want to run just this test split so we can do this like that pytest name of the file, two colons(::) and
2:45 name of the function. This will run only one function. And as you can see, this one function is passing. If we had a class here,
2:54 so let's create one. Okay, now we have to pass self everywhere. And what's the problem? Too many blank lines.
3:11 So, let's format this document and now, like it is happy. No, we still have the failing test.
3:27 Let's fix this, Now, We can either run all the tests from a class or we can specify a single file inside this class,
3:44 so we always have to use this double colon to seperate file name, class name and a test name.