Python for the .NET developer 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.
0:02 That was fun, but let's actually write our first real test.
0:05 So let's say we went to test, say, test electric guitars.
0:10 Wrong. Why wrong? Because we're not going to get rid of the dependencies.
0:15 We are just going to call it.
0:17 So this turns out to be pretty straightforward.
0:19 We can go over here and import lib.
0:21 Right, that's this library right here
0:23 that does all of the guitar stuff and call all_guitars.
0:27 We can come and say guitars = lib.all_guitars.
0:31 Let's give it a style
0:33 and let's say that the style is equal to electric
0:37 because we're going to want to use this
0:38 below as well, eventually.
0:40 We can do a suite, a little generator expression.
0:47 Remember we talked about list comprehensions
0:49 and generator expressions and so on?
0:51 And we could do something like this, assert all.
0:54 This is a built-in that tests
0:55 for every one of these, this thing is true, all right?
0:58 So we can say that g.style equals style for g in guitars.
1:06 So what we're doing is we're getting the guitars back.
1:09 If this is working correctly
1:11 it's going to give us only electric guitars.
1:13 So we could say all the styles here
1:16 and we're go through every guitar
1:17 and we're going to test, that for every one of them
1:19 we get the true statement that
1:21 yes, the style is the style we're expecting.
1:24 We could also do something like this. Create a set.
1:29 It's going to be something like g.style or g in guitars.
1:34 That's another one that we could do
1:36 and maybe I'll just print that out
1:37 so you all can see what that is.
1:39 But I kind of like the first one
1:40 but we can just print them out so you can see
1:42 there's a couple of varieties here.
1:44 Most importantly, we're going to run this and see what happens
1:46 so let's run it. Look, our test passed.
1:50 Electric guitars, wrong. It looks like it worked
1:53 and know we printed out this thing
1:55 so what this does is this takes
1:57 all the styles in this collection
1:59 and then, it reduces them down to just the unique elements
2:03 like a HashSet like we did in .NET.
2:05 Same thing here, those both pass
2:08 but I definitely like this one best.
2:10 I think it's the clearest.
2:11 Every single style is equal to the style we expected
2:14 which as you can see, we're getting back to the electric.
2:17 Run it one more time. Woo-hoo, it worked great.
2:20 Well, it worked great except for we're logging
2:23 and we're going to the database.
2:25 Go ahead and clean that up.
2:26 Leave you just with the essence here.
2:28 Let's create that small, create another copy of it
2:32 to work on here in a second.
2:33 I'll leave it commented, but this is not good
2:36 because we're using our dependencies.
2:38 What did we do in .NET?
2:39 If you recall, this lib
2:42 we actually allocated it in a sense.
2:44 It was something like this
2:47 and we said we're going to have a mock log
2:50 and a mock DB or something to that effect.
2:53 We're not doing that. In fact, we can't even do that.
2:55 This is just a module, not a class.
2:58 What are we going to do? Well, Python has some really simple answers
3:01 on how to write this test correctly.