Getting started with pytest Transcripts
Chapter: pytest Fixtures
Lecture: Using multiple fixture levels

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So to fix this, we did a delete all within the test but I'd like to do it at the fixture level, so but the fixture is being used for both tests,
0:11 so let's add another fixture in the mix to solve this problem. So I'm gonna grab all of the code that we had and go into multi level
0:20 because that's what we're gonna be doing multilevel fixtures and this sounds complicated, right? You know, suddenly we just learned fixtures,
0:26 man, what are you doing going to multilevel already? But bear with me, this is going to be pretty good.
0:31 Um we just copy and pasted so I'm sure it still fails pytest just or it should actually work, but everything's working.
0:42 But I'd like to make sure that the it's empty at the fixture level, but I don't want to change this back to function because I want this to just
0:51 run once and the close to only one runs at the end. I just want to put this in here.
0:57 This is only gonna if I if I just take this this line and make sure it's empty. Doesn't really do anything.
1:04 Doesn't really help at all because I already it's already just an empty directory, it's going to be empty and this only happens once per module,
1:13 not per function. So I want this delete all to happen once per function and for that to happen, let's just put another fixture in.
1:22 So I'll just grab, grab that def dB fixture cards DB. Well these are these can't be the same name. So I could do this one to be empty empty cards DB.
1:37 And then cards_db delete all. So this would work. So what's happening here is empty Cards_dB. Is depending on Cards_db. And then it's deleting all.
1:53 Oh I have to return cards_db and then I could change then all the tests would change and they'd have to change to use the empty one.
2:04 But I just kind of wanted to always be empty when a test gets it. I don't want to change the name because I don't want to change everywhere.
2:12 I use cards DB through here and maybe I've got dozens of tests by now, I don't want to have to just do a search in place.
2:18 I mean it's not painful but I don't want to do it. So let's change the name here. It just changed the initial cards_db,
2:26 to cards_db module module and then cards_db module. We have to change it here dear and here. So and then we move that back to Cards_db.
2:46 So are the tests, the tests are all depending on this one now. Instead, I just changed the fixture underneath them and Cards_db
2:56 is dependent on the DB module and hopefully everything just works works smoothly. And since I'm not, I don't really need to tear down at the,
3:06 at this level. I'm just doing, delete on the setup. I can just do a return and this will still work
3:12 the tear down for the the parent level module fixture will still run at the right time. I hope we'll see.
3:20 Is this enough? We've got this running this one's cards, cards_db, calling cards_db module doing the set up tear down and the rest of
3:29 the test should just run. Let's cross our fingers and hit go and see what happens. Sweet. Okay. Anti climactic though.
3:37 I mean I'm really excited that that worked actually but let's let's watch it in action so set up show we can use to see what happened.
3:49 So yes, the card, the cards_db module was run and then cards_db fixture. Oh it's still module scope. So what's going on? How did this pass?
4:02 Oh I'm still deleting all within the tests so delete all, delete all and and so that should fail,
4:14 yep, that fails because I copied and copied and pasted the module scope here. We want the cards_db that does the delete all to be at the function scope
4:26 So between before each function call the delete all to make sure it's empty. Now I think I think we're good.
4:35 Yeah. Sweet. So now we've got the module level fixture running at the beginning
4:43 and then the cleanup at the end and then between tests were just running these the cards_db it still says tear down,
4:51 We don't have any tear down but if we did it would run there. Awesome.


Talk Python's Mastodon Michael Kennedy's Mastodon