Getting started with pytest Transcripts
Chapter: pytest Fixtures
Lecture: Using fixtures to pass test data

Login or purchase this course to watch this video and the rest of the course contents.
0:00 As the first example of fixtures, I'm gonna take this simple test, I've got test cube, which checks, I've got a number 42. So this is my given stage.
0:12 I assigned 42 to a number and then I'm taking the cube root of that number with the star star and then I expect it to be the same as multiplying
0:23 the number three times. And so I'm asserting here, given a number is 42. When I cube it the output,
0:33 I assert that the output is going to be the same as multiplying it three by itself three times. This is not that complicated but let's go
0:44 ahead and run it just to make sure it works. So yes that passes, this is not a surprise to us. What happens if we, you know,
0:51 do something else? Let's not the same as four times. Yes. Just make sure that we understand that that fails and that's a pretty big
1:02 number. Okay, put that back to normal. Clear now. How can fixtures help here? Well, the 42 getting ready is right in the test,
1:16 we could push it out of the test and this is a kind of a simple example but I wanted to show you the structure of how we use fixtures.
1:24 So we have to import pytest first and then we use a decorator called
1:28 pytest fixture and just decorate a function and it can be really any function and the return value, it does action and it has a return value.
1:38 If you don't return anything of course and python it returns none. But here we want to return the value because it's going to be used by the
1:45 test and we just put the name of the fixture right is a parameter to the test and when the test runs,
1:53 so pytest is gonna see this and it is gonna see the num and it's gonna say, hey I need a fixture called that. So it's going to find this num fixture.
2:01 Run that take the return value and fill it in for the parameter. It seems kind of cool and it is cool.
2:09 So let's try to run that just to make sure that works. Pytest test cube.py, no just cube fix. Yeah,
2:22 that works too. In this case it doesn't make a lot of sense to pull this out I am just showing the structure of how to use fixture.


Talk Python's Mastodon Michael Kennedy's Mastodon