Getting started with pytest Transcripts
Chapter: Parametrization
Lecture: Parametrizing functions with two parameters

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now finally let's take a look at function parameterIzation. So we're gonna take this one test that let's fix that so that actually passes
0:10 if we try to run it again. That starts against done. So we'll take this test and turn it into a function
0:17 parameterIzation instead. So it's this is not too hard it just doesn't give us the detail and the information we want.
0:23 So it's still we're just still gonna do one test but we're gonna parameterize it the right way. First off we need to import pytest and then we will
0:36 decorate it with pytest mark, parameterize and then we have to give it a list of things we're parameterizing
0:45 So in this case we've got both the summary and the state that are different So we'll pass in both those summary state and it's the starting summary in
0:58 the starting state. We could specify that or we could just leave like this I think this is fine. So for each of those we're gonna have a list of
1:06 a set of those. So a list of we've got the list and this will be For the first one. We'll just do it's gonna be 123. And todo in Prog done.
1:24 That's what we want. So delete that that's really what we want and we just need to have the well it's kind of all we could probably put
1:36 all this stuff here. So that's not bad. So that's a parameterize summary state. 1 2 3 to do, in prog, done.
1:45 So each of the times through the list we're going to pass in this pair to the test so I need to give it the the parameter names,
1:55 summary and state. And then we will go back to assigning just one card. So C equals card summary state. Oh we need state equals state.
2:11 We don't need to specify the first one. We can if we want summary equals summary in the summary.
2:22 Equal summary. This is may be confusing if you haven't seen this before this is the parameter name and this is the value.
2:29 So let's actually put start here so we're not confusing ourselves. Start summary and start state. And then now the value of state that we're passing
2:44 in card is start state and start summary there. Now we can go back to this here we go. Inventing looks weird. Well so now we've got is this it.
3:03 All right. This list parameterize and the parameters test, finish, start summary. Oh this is wrong. Start summary Start state start summary,
3:19 start state. They get matched up with here and let's go actually let's print it just to make sure because this I don't know if I trust it print let's
3:29 do an F string start summary equals start state vehicles. pytest. Let's just make sure this works.
3:46 Oh yeah it works a little worried there for a second. If we do verbose it will show up.
3:53 This is neat actually. So that the last one when we just did the combined test Func combined. No, not test.
4:01 Func combined combined finish. That's right. Just so showed one and now we're showing that the different test cases and it shows
4:13 that with test brackets and that's what parameterization looks like you got one todo 123 and todo in Prog done. Yeah. So let's go through that top,
4:23 walk through it again just to make sure that we're all understanding what's going on.
4:27 So each time through the test this thing one is gonna get assigned to start summary
4:33 to do is gonna get assigned to start state and then it will run through it And then it does the same thing with the second set and then the same
4:41 thing with the third set. And let's go ahead and print these out just to make sure it's clear as mod wrong. One finish func params. There we go.
5:00 The print out in each one of these. It's got the it's got the test node name and then it's printing start summaries,
5:06 one star status to do is to do 123 to do in Prog done. They're pretty easy. Don't you think? Let's let's split that so we can compare those.
5:18 It's not too bad. Oh I can see one thing that right off the bat that we're we're sort of doing too much work then we need to.
5:29 So this is we're parameter rising to things. We really only need two parameters. One. I'll show you that next, but I think it's pretty good.
5:39 The benefits far outweigh the downfalls. We don't need this Print summary anymore.
5:45 We're pretty happy with that. So let's take a look at how we can simplify this with just one parameter.


Talk Python's Mastodon Michael Kennedy's Mastodon