Modern Python Projects Transcripts
Chapter: Testing code
Lecture: pytest options

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


Talk Python's Mastodon Michael Kennedy's Mastodon