Modern Python Projects Transcripts
Chapter: Testing code
Lecture: pytest options
0:00 Let's see what else we can do with the pytest command.
0:03 If we run pytest --help,
0:06 You can see a huge list of different parameters.
0:18 That's a lot of parameters. But the most important stuff is at the top,
0:23 a very useful parameter. Is this -k?
0:26 It lets you narrow down which test you want to run.
0:29 Let's say you have a lot of tests,
0:30 but you only want to run one of them, so you can specify the name of
0:34 that test with -k and the name of the test.
0:38 So in our example, let's say we want to run this test_upper.
0:47 And as you can see, only one test was selected and two were deselected
0:51 because they didn't match this expression and we don't even have to specify the full name
0:57 of the test. We can specify part of the name and all the tests that
1:02 match this will be run, so we have two tests that have upper in the
1:06 name. If we want to run both of them,
1:09 we just do pytest -k upper.
1:13 And now we have two test selected one
1:15 deselect and we can even revert this match, so we can say we want to
1:20 run all the tests that are not matching the upper in their name.
1:25 So, in this case, we have to do -k
1:28 "not upper". Okay, we have to provide double quotes now.
1:34 Only one test was run. You can specify -v parameter to get the more
1:39 with both output, so we can see which test was actually run.
1:43 And as you can see, our test_split was run.
1:46 But the other two tests that have upper in the name we're not run. Next,
1:56 We have mark so we can mark some test with,
2:00 let's say, categories or tags,
2:03 and then you can use those markers to run those specific tests or to not run
2:07 those specific tests. This works kind of like this -k parameter,
2:11 but it can be used, for example,
2:13 when different tests don't share the common part in their name.
2:17 Typical case is, for example,
2:19 To mark Some tests are slow and Only run them from time to time because
2:23 they are really slowing down. You're test.
2:26 So let's go back to our pytest example and let's mark this test split as
2:31 a slow test. So all we have to do is to decorate it with
2:36 pytest.mark and the name of the mark.
2:40 So let's let's say it's a slow test.
2:45 Now we go back to the terminal and let's run only the slow tests.
2:56 As you can see, we run only,
2:57 this one slow test. We also get this warning saying that pytest.mark.slow
3:03 is an unknown mark. That's because pytest also have some built in marks.
3:08 So it's trying to tell you when you're actually using an existing mark,
3:12 and when you make a type and just like with -k parameter,
3:15 we can also run all the tests that are not slow by saying -m not
3:21 slow. This is very useful when you have some tests that are slow and you
3:26 don't want to run them on your local computer.
3:29 Maybe it's a full end to end test that test your whole website,
3:32 and it takes a few minutes to finish.
3:34 So instead of wasting your time each time you run the whole test suit,
3:38 you can mark them as slow then pytest do not run them on your local
3:42 machine and only run them on your continues integration server.
3:49 We also have -x parameter that will exit after the first failed test.
3:55 So let's actually make a failed failing test.
4:00 Let's get rid of this unknown mark and let's rerun it.
4:06 And as you can see, only two tests have been run.
4:10 This guy was run. This guy failed,
4:12 and since this guy failed, this was not run.
4:17 We can also run pytest --pdb,
4:20 and this will automatically start at debugger session when assertion fails or if there is
4:25 any other kind of error. This is one of my favorite commands because you don't
4:29 have to manually set a break point.
4:32 You just add this one argument,
4:35 and whenever you have an exception,
4:37 pytest will automatically started debugger so we can poke around the code and see what
4:41 went wrong. So when you have a failing test and you think you fixed it
4:47 you can run pytest --lf.
4:50 which stands for last failed,
4:55 this will Only rerun the test that has failed in the previous run.
4:59 So this is a great command when you don't want to run your full test But
5:03 you just want to see if you manage to fix the failing test.
5:20 There's also --ff,
5:22 which is very similar to the previous command.
5:25 But this one will run the failed test first,
5:28 and then it will run all the other tests.
5:31 So this one is actually exactly like the lf.
5:34 Except that it runs all the other test after the failing tests.
5:38 And if you have created a new test file and you want to run this new
5:42 test file first, there is --nf option that will run all your test.
5:48 But first it will run tests from the new files that you have just added to
5:52 your test suit. In this case,
5:54 it doesn't make sense because we specify only one file.
5:57 So usually you would run it with just pytest on all your tests.
6:07 And one really cool option is this durations.
6:10 It will report the slowest test,
6:12 So you run it with pytest --duration = n
6:13 and this n specifies how many slowest test you want
6:19 to see. So in this case,
6:21 I want to see to slowest test.
6:23 But since all of my tests are faster than five milliseconds.
6:27 I don't really get the output.
6:29 This is a super useful command when you have a lot of tests and they become
6:33 slow and you want to actually see which one are the slowest,
6:37 so we can go and figure out how you can make them faster.
6:43 So this list of pytest parameters is really huge,
6:46 and you can easily customize many aspects of pytest.
6:50 So I recommend you take a look from time to time because each new release of pytest brings new features.