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

Talk Python's Mastodon Michael Kennedy's Mastodon