Modern Python Projects Transcripts
Chapter: Testing code
Lecture: Marks

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We saw how to use marks in pytest to mark,
0:02 Some tests are slow or two parametrize them.
0:06 So let's talk about mark in more details.
0:08 Marks are like categories for your tests.
0:11 Let's say that in your code you have two different types of tests.
0:15 You need tests that test a very small functionality,
0:18 and they run very fast and end to end test that test your whole application.
0:23 But they take quite a lot of time to run.
0:26 Since your end to end tests are slow,
0:28 you probably don't want to run them together with all the other tests on your computer.
0:32 It's much better to run them on a continuous integration server each time you create
0:37 a pull request. So you mark your end to end test with slow marker and
0:42 then you exclude them from running,
0:43 by passing -m not slow parameter to pytest as we saw in one of the
0:49 previous lessons. But if you use marks like that,
0:54 you have to remember to register them.
0:56 Otherwise, you're gonna get this warning from pytest saying that Hey,
1:00 you're using an unregistered Mark,
1:02 make sure this is not the type of. So if you're using pyproject.toml,
1:07 you have to add an argument called markers,
1:10 and there you specify a list of markers that you want to use in your test
1:14 You can just specify the name of the marker,
1:17 like with the serial. Or you can also specify the description of what this marker
1:22 is supposed to do. pytest also comes with some built in markers.
1:29 For example, we have this skip marker that you can use to disable a given
1:33 test. This is useful when you're in a hurry to fix something in production,
1:37 but you have some tests failing.
1:39 So instead of removing them or commending them,
1:42 you can just add this mark and they will be display in the test report.
1:46 as Skipped, That way, you still get a hint that you should fix them
1:51 at some point in the future.
1:52 There is a similar marker called skipif,
1:55 but this one lets you specify a condition when this test should be skipped.
1:59 For example, if you want to test a piece of code that will only work
2:03 with python 3.6, you can market like that now,
2:06 When you run your test pytest will try to evaluate this expression,
2:10 and if it evaluates to true,
2:12 it will skip this test and display the reason in the test report.
2:18 If a test is expected to fail,
2:20 you can mark it as xfail and pytest will ignore it, when it actually fails.
2:25 Why would you want to use a mark like this?
2:27 Well, maybe you have a test that is currently failing,
2:30 but someone is already working on a fix,
2:32 and it should be patched in the next few days.
2:35 So instead of skipping and removing this test,
2:37 you mark it as expected to fail.
2:40 When the fix is implemented and your test will start working again,
2:44 you will see a warning message from pytest saying that it was expected to fail,
2:49 but it actually passed. So that way you know that your test has been fixed
2:54 and you can remove the xfail
2:55 Mark, all those three marks. So skip, skipif.
2:59 and xfail, accepts an optional parameter called reason,
3:03 you can write down the reason why a given test is skipped or why it's expected to
3:08 fail, and this reason will be printed in the pytest report.