Getting started with pytest Transcripts
Chapter: Parametrization
Lecture: Using ids for parameter objects

Login or purchase this course to watch this video and the rest of the course contents.
0:00 In the multiple fixture example. We had two different fixtures that were both parameterized. and we ended up with six values.
0:09 But what if we didn't want that, what if we wanted to use fixture parameterization but we really did want to have the summary and state pair it up.
0:18 In that case we can use one fixture, we're gonna have one fixture and we're gonna call it state summary because it could have
0:28 starting state starting summary but that would be kind of long. So we'll just have it state summary for the params I'm gonna give,
0:35 each param is going to be a tuple so it's gonna be a tuple of one to do two in Prague three done now I'm not really going to do it,
0:44 I could do something, I could unpack them within the the the fixture to do work if I needed to but here we're just going to return it.
0:52 So what this is going to return is going to return a tuple of values and then the the test is going to have to,
1:00 if it wants to use them separately we need to unpack. So this line is just unpacking the tuple into a start state and a start summary
1:08 and then passing the rest on just as we did before. Now that we have we know that we can do really anything within params it doesn't
1:16 have to be just strings or just managers or small individual values. It can do be things like tuples.
1:24 It can also be objects. So let's take this one step further and say fix your object. So this is the same,
1:33 it's really the same test. But instead of a tuple we're going to just go ahead and do the card now in. I can just say start card.
1:43 So now I'm going to iterate through here and I'm going to have card one card two and card three and I'll just return the card.
1:51 So it's just start card it'll get returned to the test and now I don't need to create card within the test because it's already created.
1:59 I can just pass it on to add card. Now let's verify that both of these work pytest test fix multiple one fixture and
2:11 test fix object sweet. Both of them work. And if we do have verbose, hmm we have a little bit of a problem.
2:22 So the little bit of a problem is that's not very interesting. Start card zero start card one start card two.
2:29 And same with the state summary state whatever here. 1012. This does distinguish them but it's not that interesting.
2:40 And that's what pytest does with any object or any high level thing if it's not an obvious thing, how to print it just does that 012 thing.
2:49 So what do we do? We have an ID's. That we need to use. it's an extra feature of fixtures and my there's a lot of ways to do
2:58 it. I cover a whole bunch of this within the py textbook. But the easiest thing. Hopefully if you're comfortable with lambdas is to throw a lambda
3:07 in there so ids Equals. And then I'm just gonna do a lambda expression lambda and each of these is going to get an item like passed to it for each one.
3:19 Each idea it's gonna be a function that takes one value and it needs to return a string. So lambda X.
3:25 And then return X.say summary If we do that that will fix this one does 123. So that's an ids. with a lambda let's show what
3:40 ids Looks like in the multiple fixture case it's gonna be let's do a different function. So you can do lambdas.
3:48 If you're not comfortable with lambdas you can just define your own functions. So id Func I usually do id Func for, it's just a habit of mine.
3:58 And let's also give it X. Which is terrible variable name but you know habits so that's gonna get passed in one
4:05 of these tuples. And so if that returns if that returns the 1st one zero that should be good if I want to do like the the both of them together
4:18 I can do that too. So let's let's go ahead and return F. This to an F String of X[0]- X[1] and now the same thing I pass it into the fixture and do ids
4:37 IDS Equals id func. Now they're readable. I did the dashes in in the in the one case and just picked the 123 because they're different.
4:54 They're different but they're that's like not meaningful. Right? It's the wrong one. We don't really care about that do we?
5:00 We could do the same, we could do the ids. Or we could pick the pick the state instead. Now it's todo in prog done.
5:10 That's a little bit more interesting. So that's how you deal with passing objects or tuples or other things or multiple
5:18 parameters to one fixture for parameterization.

Talk Python's Mastodon Michael Kennedy's Mastodon