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