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