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

Talk Python's Mastodon Michael Kennedy's Mastodon