Getting started with pytest Transcripts
Chapter: Parametrization
Lecture: Fixture parametrization

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Alright. So this is function parameterization again this is the one with one parameter
0:07 and it starts state and we're going to take this as a starting point to show
0:11 you fixture parameterization. So this is function parameterization were parameterizing. The test function with fixture parameterization.
0:20 We are parameter rising a fixture. So we need a fixture. We're going to have the fixture be the start state
0:26 and we're just gonna move this start state out of here and put it into a fixture. So to make a fixture of course we do pytest fixture.
0:35 There's parameters you can give to pytest fixture and one of them is there's kind
0:40 of a whole bunch. We're not gonna cover all of this in this course but one of them is params and with params we just pass in the parameters that we
0:50 want to use a list. That's it. Now we need a fixture so that's how you parameters.
0:59 The fixture will make a fixture start state but to grab this we need to be able to grab the the value out and to do that. We'll use a built in fixture.
1:13 That pytest has called request and in the pytest book. I kind of talked about this a bit for now we'll just trust that we can
1:21 get the parameter out of here and we get it out of there by we want
1:25 to return it request param. It's important for fixture parameterization to actually return the value because I assume that's why we're returning it.
1:36 I mean I guess we wouldn't have to if we're not using the value of start state within the test. We can we don't have to but we are we're
1:44 using it within the test so we need to return it and so we don't need this parameter rise and so that's it.
1:51 We take that off, we don't need this one start state stays there because it's
1:55 the the fixture and before the test this will run and the value gets return to the test and we can use it within the test.
2:06 That's it. Let's go ahead and run that pytest -v test fix param there. We have it. It's just kinda like we did before.
2:17 It just shows the actually we could even run uh the other one just as a comparison text test func one param. It looks kind of the same,
2:29 right? It just has the test nodes, let's pull this up so we can look at both of them is very similar. It's just a different different file.
2:40 So fixture and function parameterization are kind of indistinguishable from the test node name standpoint
2:46 But what's different about them in action when this runs before it runs the fixture gets run. We can even I mean,
2:55 let's go ahead instead of walking talking through it. We can use setup show to, show what it's doing, wow, it's doing a lot. Let's get that one again,
3:13 whole bunch tmp path factory. All right. And then the db card session and then the cards_db.
3:19 And then the start state and then the test and then we unwind part of it We unwind to go back to the Cards_db.
3:27 We're not doing this session and factory except for at the end again. Cool. That's nice. It's a lot of information there.
3:34 I kind of forgot about all that. One of the things that I wanted to show is the set up and tear down
3:40 of start state. We're not really doing anything here, but we could if we had some work to do, we could do setup work here. And if we had tear down,
3:52 it's not going to run with a return, but we can do a yield and now it will run.
3:59 So that would be how you do a set up and tear down within fixture parameterization. But for right now, we will just return the value.

Talk Python's Mastodon Michael Kennedy's Mastodon