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


Talk Python's Mastodon Michael Kennedy's Mastodon