Getting started with pytest Transcripts
Lecture: Skipping tests conditionally with pytest.mark.skipif
0:00 Now let's say I've decided that this sorting and the less than I'm not going to support it now, but I'm gonna sort it support it in version two.
0:09 So right now the cards version is 1.0.1. And I'm gonna I think that the next major version will support this sorting for.
0:19 I don't know, I'll come up with a reason why we need to go ahead and copy this into a test skip if but I want to show you what we're
0:26 going to use to compare the version. See the version string is just a string. It's like 1.0.1. Let's take a look at it.
0:36 It's in the init and it's just this string. So I want to compare the version to say that this major version is less if
0:44 it's two or if it's less than two, then I want to skip these tests. That's the comparison I want to do to get at that.
0:51 I'm going to use something from packaging. So it's uh the parse function from packaging.version. And packaging is a third party installed.
1:01 You have to do. So you're gonna have to do pip install packaging to use this. But I want to show this to I've already got it installed.
1:10 I want to show it because it's really pretty cool and I've used it in a couple of projects. How it works is it's probably got a bunch of stuff
1:18 in there. But what I use it for is to pull out the major minor and micro versions so that I can see those and I have this test put together
1:26 I've got a print so that we can see it. And I'm just asserting that if I put it all back together the major minor micro
1:33 with dots it will equal the version that we had before. So let's go ahead and run that just to see how the packaging version parse works
1:42 So pytest -S test, skip if. Major is one minor. Zero micro is one and I'm pretty sure that test passed the packaging.
2:00 Yes it passed. Let's just leave that in place while we're playing with the rest Now now that I have that ability to parse that out I can say give
2:09 this a condition. So instead of skip I'll say skip if and then the first thing that skip if wants is a condition and I'm going to give it this
2:18 say that this parse command. If this is the object that gets passed back I'm
2:25 just gonna look at the major and if that's less than two then we'll go over Go ahead and say sort not supported on version one x.
2:38 And then we can do the let's go ahead and just copy that. It's gonna be a little so less than not supported.
2:51 So now we have skip ifs instead of skips and we were skips before let's run
2:59 it again. And less than and support not supported on version 1.x wonderful so that's how skipif works.