Effective PyCharm Transcripts
Chapter: Unit testing
Lecture: Testing failure conditions

Login or purchase this course to watch this video and the rest of the course contents.
0:00 When we look at our code here, both the tests and the actual program itself. The library. This core library.
0:07 It really works well when you go down the specified path, I'm supposed to be able to go and see if there's a table get it.
0:14 I'm supposed to be able to book a table but it's missing all of its error
0:18 handling. And one thing that I've learned over time is that when people are new
0:22 to programming they just focus on getting it started making sure the thing is supposed to
0:27 do. It does. But the real difference of polished software is that it deals
0:32 with data that's not correct inputs that are not valid situations that didn't make sense.
0:37 Like trying to book a table that already has been booked or trying to book a table that doesn't exist or something like that.
0:43 So what we're gonna do is going to write two more tests that verify that we're checking for those things. So let's go over here and write a test.
0:54 The first one says you can't book a table that doesn't exist. We want this to respond in a meaningful way. How are we going to do that?
1:04 We're just gonna go down here and say core right? There's a book a table. And if you look at what this value here is supposed to be it's a string.
1:12 So we're gonna come down here and let's give it some ID. That's not going to work. Right? So obviously this should fail.
1:20 What's going to happen. How should it fail? Should it return none should it raise an exception. I'm gonna go with raises exception.
1:26 Right? That's a pretty Python equate to go. In fact, if we go down here and look at book a table notice there's
1:34 a integer not found table unavailable error and all those kinds of things to the error
1:39 handling. An exception support should be there for us to do this. Right? Let's give it a run and see what happens.
1:45 Uh It didn't pass what is what is going on here? All right. So what we got was it this failed? And why did it fail? But an attribute error?
1:57 This is probably the most common error in all the Python attribute or none. Type object has no attribute is booked. This is not a meaningful error.
2:05 This is just our code crashing halfway through its method. That's what we did is we had no table.
2:10 Then we tried to book it and obviously if it doesn't exist, this is going to crash. So what we need to do is actually write some
2:18 tests in our code to check for this. For example, we should say if not table. What do we do? Let's raise an entity not found error.
2:30 All right. So that will avoid. If there's no table us getting that attribute error here, let's try that. Okay, It's still crashed.
2:37 What's going on. Let's see. Oh, perfect. We did get this core attribute not found error, but why did it crash? Well,
2:46 this comes down to how we're writing our test. What's supposed to happen here in a normal test. If there's an exception that's a failure.
2:54 But in one of these negative error condition tests, that's exactly what we want is a failure.
3:00 If there's not an error, it's like a double negative of testing. So what we can do is we can come over here and now notice so far
3:07 we've not even used pytest but now we can start working with py tests for its extra features and we can create a context manager.
3:15 So we say with pytest raises and then you specify the type of exception. Then anything that happens in there.
3:22 If it leaves us with block without raising that error, that is a error. That's a failed test. So that that's the double negative. So now if we run it,
3:31 let's see what happens. Boom, it passes. This one passes. Why? Because we said it must raise an error and guess what it did.
3:39 Let's do one more of these negative tests and that is that. You cannot book a already booked table. They were down here again. Let's get a table.
3:51 We'll go down the course a all tables Bracket zero. I'll just give us the first one quote up,
3:57 book the table the table id and actually let's again just to make sure so we
4:03 can make sure that we're not going to get one that's already booked as we want to make sure this works smooth every time.
4:12 So we're going to book the table and then we want to try to book at a second time. So we'll go to the flow of somebody,
4:19 books it not us and then later table id just like before this should be an error. Let's say this time. We want a core table unavailable error.
4:37 We like that. Okay, so this should work pretty easy. Let's go ahead and do a little time to clean up, make it run it failed again.
4:46 Why did it fail? Because it didn't match the double negative this time it did not raise any error and it did not raise that one in particular.
4:54 So let's go back in and see what's going on here. Book a table again. Look, it seems like we might have just double booked it,
5:02 it was booked before and now it's still booked. So we don't want to let that happen. We're gonna say if table dot is booked,
5:10 raise that error, Run our tests again. Hey, that did it now are two negative scenarios in our two positive scenarios pass
5:20 right? We cannot book a book table. We cannot book a non existent table but we can book one and there are some that could be booked beautiful.


Talk Python's Mastodon Michael Kennedy's Mastodon