Python for .NET Developers Transcripts
Chapter: Testing
Lecture: The first real test

Login or purchase this course to watch this video and the rest of the course contents.
0:00 This silly assert number equals number. That was fun, but let's actually write our first real test.
0:06 So let's say we went to test, say, test electric guitars. Wrong. Why wrong? Because we're not going to get rid of the dependencies.
0:16 We are just going to call it. So this turns out to be pretty straightforward. We can go over here and import lib. Right, that's this library right here
0:24 that does all of the guitar stuff and call all_guitars. We can come and say guitars = lib.all_guitars. Let's give it a style
0:34 and let's say that the style is equal to electric because we're going to want to use this below as well, eventually.
0:41 We can do a suite, a little generator expression. Remember we talked about list comprehensions and generator expressions and so on?
0:52 And we could do something like this, assert all. This is a built-in that tests for every one of these, this thing is true, all right?
0:59 So we can say that equals style for g in guitars. So what we're doing is we're getting the guitars back. If this is working correctly
1:12 it's going to give us only electric guitars. So we could say all the styles here and we're go through every guitar
1:18 and we're going to test, that for every one of them we get the true statement that yes, the style is the style we're expecting.
1:25 We could also do something like this. Create a set. It's going to be something like or g in guitars. That's another one that we could do
1:37 and maybe I'll just print that out so you all can see what that is. But I kind of like the first one but we can just print them out so you can see
1:43 there's a couple of varieties here. Most importantly, we're going to run this and see what happens so let's run it. Look, our test passed.
1:51 Electric guitars, wrong. It looks like it worked and know we printed out this thing so what this does is this takes all the styles in this collection
2:00 and then, it reduces them down to just the unique elements like a HashSet like we did in .NET. Same thing here, those both pass
2:09 but I definitely like this one best. I think it's the clearest. Every single style is equal to the style we expected
2:15 which as you can see, we're getting back to the electric. Run it one more time. Woo-hoo, it worked great.
2:21 Well, it worked great except for we're logging and we're going to the database. Go ahead and clean that up. Leave you just with the essence here.
2:29 Let's create that small, create another copy of it to work on here in a second. I'll leave it commented, but this is not good
2:37 because we're using our dependencies. What did we do in .NET? If you recall, this lib we actually allocated it in a sense. It was something like this
2:48 and we said we're going to have a mock log and a mock DB or something to that effect. We're not doing that. In fact, we can't even do that.
2:56 This is just a module, not a class. What are we going to do? Well, Python has some really simple answers on how to write this test correctly.

Talk Python's Mastodon Michael Kennedy's Mastodon