Getting started with pytest Transcripts
Chapter: Markers
Lecture: Selecting tests with custom markers

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now I'd like to show you how to do custom markers. So custom marker is like a tag or label that we apply to our test just
0:10 so that we can reference a set of tests by themselves. So let's take this example. So this example I've got in in chapter five it's
0:18 test custom and we're also going to use the pytest any file. There's nothing in it yet. Test custom is from test card from Chapter two.
0:27 It's a bunch of original tests that we just were playing with. I'd like to take a look at this and there's a few of these that are
0:36 similar. So we've got testing field access test defaults and then we have equality inequality with different ids
0:45 And then inequality. And if I want to run just these are the these tests I could do something like pytest mark.
0:56 And like when we were doing skip it would just be skip here. But I actually want to run these.
1:00 So I'm just gonna mark this with equality and then use the same mark on these three different tests. That's the part that you do to the test to add a
1:12 marker to your own tests. But there's something that will happen if we run this just as it is. Let's make sure we're in the correct directory.
1:23 And we are. So we'll run pytest test custom and oh pytest is not defined. That's the first error.
1:34 So we'll have to import by test but then we get these warnings showing up for all all three of these that we had. And it is saying unknown pytest mark,
1:51 equality. Is this a typo and then it gives us a web page to go to if we want to register custom marks and I'll just show you how to do
2:00 that right now. So to register custom mark, that's where we go to our pytest Ini file, this is your settings file will cover that in the next chapter.
2:08 Actually a couple of chapters after this. But if you don't have one already, it's usually at the top of your project.
2:15 So pytest any file and it just looks like this. It'll say pytest in it and then we'll have settings related to pytest and
2:23 we're just gonna add a markers setting, we're gonna give it a multiple line because I'm gonna have a couple of these equality
2:33 and then we give it a description for what this markers for. So these are tests for equality and inequality. That's all we have to do to register it.
2:44 Now these warnings go away. What did we do? Nothing so far We have it we're just adding markers to these tests and then we
2:54 registered the marker or you could do it in the other order of course and then
2:58 they have marks on them. But if we don't use the mark doesn't do anything But if we use the mark and we use it with the -M and we pass it in the mark,
3:07 the custom mark quality. Now it just runs those three tests. And we can actually combine this with keywords even so we can say mark equality
3:20 mark. But we maybe say we only want the ones with with diff in it Let's add another one just to show you how to combine them.
3:31 So if I do another one say foo some foo tests. And now within our test file we've got equality. And let's add foo to this one.
3:50 So inequality is foo And Let's also do the last two. So you can have marks on multiple tests and single test can have
4:03 multiple marks. So now we've got you on a few tests and equality on a few tests. And so what does that do?
4:11 We can combine these we can say equality and foo. So the ones that are both of those. So that's just one. Equality or foo is either of them.
4:25 And then we can also say like equality and then combine them in all sorts of ways. Like And not foo that will select the tests that are marked with
4:38 equality but not marked with foo. And then we can also do the reverse. Of course not equality in foo.
4:47 Custom marks are handy and they're waiting for you to select which test you want to run or which test you don't want to run.
5:00 And of course there hopefully if you've done it right there declared within your your pytest any file but you can also see them with with a marker.
5:10 So you can say pytests markers and it'll show you all of the built in markers that we can use. But it also shows at the top of the list
5:21 is our custom ones and it even shows are descriptions. So that's nice. The last thing around markers that I want to show you is
5:29 that you can do strict. So what happens if we misspelled something like if we called this foe instead of foo what'll happen?
5:42 We know we'll get that warning right custom and we'll get that warning that is this a typo we can make this warning not a warning.
5:53 We can make it be stronger than a warning if we pass in strict markers and then instead of a warning we get an error.
6:06 I personally would rather have it be an error than a warning. So I like to have strict markers so in order to and I but I always
6:14 forget to like pass it in so in order to always have it passed in we can add another setting to our pytest init file addopts and I usually put
6:26 addopts at the top but I'm not sure why but we can just add it here strict markers and we don't have to pass anything in sweet but I'll go ahead and
6:39 fix that because I don't want that error in the code.


Talk Python's Mastodon Michael Kennedy's Mastodon