Python for the .NET developer Transcripts
Chapter: Testing
Lecture: Passing test data with pytest fixtures

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Now, over here we got our guitar data
0:02 because we went to the database.
0:04 Remember in quotes, our fake simulator database
0:07 but we went to the database, our true dependency
0:09 to get the data. Well, if we don't want to test it wrong
0:13 but we want to test it right
0:15 we need to somehow get some test data that we can use.
0:18 And Pytest has a really interesting way
0:21 to factor out things
0:23 like setting up little applications in test mode
0:25 or creating test data mock objects
0:29 all those kinds of things and they're called test fixtures.
0:34 I'm going to go over here and find a class called test fixtures
0:37 and I'm just going to drop a method here.
0:39 And so, this is a method that will give us some
0:42 fake data and let's just make sure
0:44 that all the imports are working.
0:48 So, what we could do, easily enough
0:50 is we could go down here and we could just call
0:53 test and data equals
0:59 import that. We could call it directly, right?
1:02 However, we're not going to have to do that.
1:06 Over here, we can say, import pytest as well.
1:10 And what we could do is go put a decorator here
1:12 and say this is a Pytest fixture. Like that.
1:16 And when we say this, we can just use
1:18 whatever that is called right there
1:20 as an argument to our test.
1:22 So, lets just go like this
1:25 and let's just print out what we get right here.
1:28 We might have to import
1:29 yeah we are importing test fixtures up there, okay, great.
1:31 So, let's run it and see what we get.
1:38 Oh, whoops, I got to change this
1:40 to explicitly import guitar_data
1:42 but once we do that if we run it
1:44 notice this passes and we go down here and click
1:47 on the one that we're seeing.
1:49 Look at this, here's our list of object, object, object.
1:52 You can bet that those are our test objects.
1:55 All we have to do is make Pytest see
1:58 our pc and then we just pass it as an argument
2:01 and it's that method is run
2:03 and the resulted value is passed into each test.
2:06 So, that's a pretty good start.
2:07 We should be able to somehow work with this method here
2:09 and pass it to test data.
2:11 But again, we saw a dependency injection
2:13 open/closed principle, all that kind of stuff.
2:15 They don't apply here
2:17 and that's not really the way we're going to do it.
2:18 We're going to use mocking in a slightly different way, next.
2:22 Now, that we have our guitar data we should
2:23 be able to make progress on eliminating the problem
2:26 that we see here like, getting stuff from the database.
2:30 We'll be able to use this guitar data
2:31 instead of the real database.