Python for the .NET developer Transcripts
Chapter: Testing
Lecture: Testing for errors

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well, it looks like were
0:01 testing for the happy path.
0:03 All the things we expected, just fine.
0:05 So, were testing if we
0:06 go ask for just electric guitars
0:08 we get those back.
0:09 Then if we ask for all the guitars
0:11 we get both electric and acoustic guitars.
0:14 A super important part of testing
0:16 is to test the air handling and
0:18 the air conditions and
0:19 the off by one errors and
0:20 those types of things because
0:22 you want to make sure that if
0:23 invalid data goes into your function or your class
0:26 that it deals with that and
0:28 it doesnt get corrupted.
0:29 It just says, No, Ive checked for this.
0:31 This is invalid. Were not going to proceed.
0:33 Or something to that effect, right?
0:34 This is really important, and
0:35 its often skipped.
0:36 Were going to have another test.
0:39 A test for an invalid style.
0:41 We dont have to have anything passed in
0:43 we dont need the data.
0:44 We also dont need our mocker at this point.
0:47 Although, to be completely safe
0:48 maybe we would pass it
0:49 but it turns out
0:50 were not going to need it.
0:52 So, lets go over here and say our
0:54 we dont even need to work with our guitars
0:55 well just say lib.all_guitars and
0:57 were going to pass in none.
1:00 None in style for which theres no guitars.
1:02 I think that should just return an empty set.
1:04 But none, is not a valid style in our world. Okay?
1:08 So, were going to test this
1:09 but what should be the outcome?
1:11 What should we assert?
1:12 We just want this to crash.
1:13 We want it to throw an exception
1:15 to raise an exception saying
1:16 No, no, no. This is an invalid argument.
1:19 Or something to that effect.
1:20 Thats what were going to do.
1:22 Now, PyTest has a way to deal with this
1:24 but lets just first run it and see what happens.
1:28 That one didnt work.
1:29 Lets see, weve got an attribute error.
1:30 This is the most common exception that
1:33 youre going to see in Python.
1:34 Its equivalent to .NETs most common exception
1:37 NullReference type.
1:38 This says, None type. None.
1:40 This says, None type. None.
1:41 You know, Pythons an equivalent of null.
1:44 Does not have a function called lower.
1:47 We passed in None.
1:48 It tried disable the style
1:49 lets lower it so we
1:50 can create a canonical representation of it
1:53 and then test it in our test here.
1:55 But it obviously didnt test for this.
1:56 So, yeah, we did get an exception
1:58 but attribute error.
1:59 This is not, this is like a, this is
2:01 almost never the right type of thing for
2:04 us to throw to indicate this.
2:06 So, lets go over here and
2:08 tell the test we want it to have that problem
2:10 so say with PyTest
2:12 which we havent yet had to import
2:14 but I told you
2:15 once you get far enough down the path
2:16 youre going to actually have to import PyTest.
2:18 So, first time were doing that.
2:20 There it is.
2:21 We can just say this raises
2:22 and give it an exception type.
2:24 And the exception type that
2:25 I think is going to make the most sense
2:27 is ValueError.
2:28 Its like, argument exception or something to
2:31 that effect.
2:32 So, were going to say we expect this to
2:34 throw a ValueError, meaning with
2:36 a message like
2:37 Hey, none is not a valued style, a valid style.
2:40 Lets see what we get.
2:42 Well, it crashed.
2:44 Yeah, it basically just didnt even catch it.
2:47 Lets go and fix this
2:48 so its not doing that.
2:49 And, obviously the problem is
2:50 were just not doing any validation.
2:52 So, here were going to say
2:53 if style is none or not style.strip.
2:59 Basically, this is the equivalent of
3:01 string.IsNullOrWhitespace.
3:03 Theres not a single function in Python that'll do it.
3:06 But these two, they'll do it.
3:07 Is it None, or is it an empty string?
3:10 And then, just going to say
3:12 raise ValueError. There we go.
3:16 So well just say whatever they passed in
3:17 whether thats None or just empty string
3:19 or whatever, is an invalid style.
3:22 Currently, our tests are failing.
3:23 Lets run it again, see if that fixes it.
3:25 Boom! Of course it does.
3:27 Our invalid style passed
3:28 so we can come down here and look at it.
3:31 We could probably turn up the logging to
3:32 get details about whats happening
3:34 but let the default level of verbosity.
3:37 It just passes, right?
3:38 So, it is passing because it
3:41 does throw this exception.
3:44 We can test that here if we
3:45 just said something like this
3:49 return empty list.
3:52 Well see that this test failed because it
3:55 did not raise this exception.
3:57 Makes a lot of sense.
3:59 There we go. So, thats like the
4:01 expected exception attribute in C-Sharp.
4:04 There we go.
4:05 So it looks like were testing both
4:07 for the happy path
4:08 and the failure path.
4:10 Because the firs thing this
4:11 method does is validation
4:14 it never gets to try to
4:15 call get guitars from DB or
4:17 log or anything like that.
4:18 So, we dont actually have to mock it out.
4:20 You know, if you want to be super safe
4:21 go ahead and throw that in there
4:22 but its really not needed
4:24 so Im not going to do it. And thats it.
4:27 This is how we write tests in Python with PyTest
4:30 to test this library right there.
4:33 We created our test fixtures
4:34 and all sorts of cool stuff to
4:36 make our life for testing much easier.