Getting started with pytest Transcripts
Chapter: pytest Fixtures
Lecture: Using multiple fixtures per test

Login or purchase this course to watch this video and the rest of the course contents.
0:00 The last thing I want to talk about with fixtures before we move on is to
0:05 talk about multiple fixtures. I know that within the built in fixture section and
0:10 others we um transitivly used a lot of fixtures because like the test one uses
0:18 Cards_db with in Cards_db use a session and indirectly we use a whole and tmp path factory and indirectly use a whole bunch of them.
0:26 But each individual place, we only used one fixture at a time but that's just because that worked for our needs.
0:33 I just want to demonstrate that you can use multiple fixtures. So this is just gonna be a pretty simple example import pytest,
0:42 we're doing this so that we can declare a fixture. just fixture thats spell right and this can be anything.
0:54 So of course fixture can be whatever return, let's just return some stuff return to let's return it slightly different,
1:03 capitalized. Maybe we need a baz fixture bar and let's just go ahead and do three. Why not complete the few more baz Okay,
1:25 so these three fixtures room here test multiple multi. Sure, that's good enough and I want foo bar and baz and so all three
1:39 of these fixtures are going to be used and can just print them, print foo just just to demonstrate that we can use multiple fixtures.
1:53 So let's let's just go and run that we're in the right directory and pytest
1:59 of course they won't print anything because we are not failing but if we do -s it'll put stuff out and all three of them got printed so that's it's
2:13 not that surprising but I guess it's just good to know that you can use multiple fixtures at at a time. You can also,
2:20 I mean at any level so they can depend on each other. Multiple fixed tests can use multiple fixtures,
2:27 fixtures can use multiple fixtures, but what you cannot do, so I can't say one of the things I have to respect scope, so if I do scope equals module,
2:39 scope equals session, this is gonna be backwards. So if Bar uses foo, I can't do that and baz uses bar, they can they can use both.
2:53 So let's say it uses Foo and bar, so what's wrong here? So this is a function scope by default, it can use anything above function scope.
3:03 It can use a session of module but bar can is session, it can't use there's nothing above session so it cannot use module So this should blow
3:13 up and it does luckily it tells us exactly what happened. You tried to access module scope, fixture foo with session scope, fixed request object.
3:29 I don't think I did that in any way case. It's a it's a scoping problem. If we swap these it'll work fine or I can do module here too.
3:40 That should work, yep, function to module two. Session or work. Yeah. And the order.
3:49 What if the order makes a difference when we were using all these three here I guess show foo bar baz and then baz bar foo.
4:01 If they're all the same, let's just leave it like that so that works and play with it. You can play with these to see how it's going.
4:08 I do encourage you to do simple tests like this, that if you forget about pytest functionality or really any library you're working with.
4:15 Just make little play test functions and test fixtures to play around with it to see if it works and have that separate from your test code.
4:24 So this is completely separate from the cards application. I'm just exploring how this functionality works. So that's a great, great thing to do.
4:33 I do it all the time.


Talk Python's Mastodon Michael Kennedy's Mastodon