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 transitivly used a lot of fixtures because like the test one uses Cards_db with in Cards_db use a session and indirectly we use a whole and
0:23 tmp path factory and indirectly use a whole bunch of them. But each individual place, we only used one fixture at a time but that's just
0:31 because that worked for our needs. I just want to demonstrate that you can use multiple fixtures.
0:37 So this is just gonna be a pretty simple example import pytest, we're doing this so that we can declare a fixture.
0:45 just fixture thats spell right and this can be anything. So of course fixture can be whatever return,
0:58 let's just return some stuff return to let's return it slightly different, capitalized. Maybe we need a baz fixture bar and let's just go ahead and do
1:16 three. Why not complete the few more baz Okay, so these three fixtures room here test multiple multi.
1:30 Sure, that's good enough and I want foo bar and baz and so all three of these fixtures are going to be used and can just print them,
1:43 print foo just just to demonstrate that we can use multiple fixtures. So 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