WEBVTT 1 00:00:00.390 --> 00:00:02.000 Hi guys and welcome back. 2 00:00:02.000 --> 00:00:03.090 In this video we're going to learn 3 00:00:03.090 --> 00:00:04.490 how to test for errors 4 00:00:04.490 --> 00:00:06.037 that may happen in your functions. 5 00:00:06.037 --> 00:00:07.240 So, what we're gonna do 6 00:00:07.240 --> 00:00:09.370 is we're gonna take our divide function, 7 00:00:09.370 --> 00:00:11.540 we're gonna say if divisor is zero, 8 00:00:11.540 --> 00:00:13.452 we're gonna raise a ValueError 9 00:00:13.452 --> 00:00:16.203 and we're gonna say The divisor cannot be zero. 10 00:00:17.130 --> 00:00:21.260 Now what this means is if we pass in a divisor of zero, 11 00:00:21.260 --> 00:00:22.489 we need to have a test 12 00:00:22.489 --> 00:00:25.209 to see that ValueError is raised, 13 00:00:25.209 --> 00:00:26.930 so let's implement that test. 14 00:00:26.930 --> 00:00:28.580 This is also gonna be helpful for you to learn 15 00:00:28.580 --> 00:00:31.000 how to check for errors in your tests 16 00:00:31.000 --> 00:00:33.540 in case you wanna use that elsewhere. 17 00:00:33.540 --> 00:00:36.140 So, I've created a test called test_divide_error_on_zero 18 00:00:37.210 --> 00:00:39.280 and the syntax for checking an error, 19 00:00:39.280 --> 00:00:42.310 the easier syntax is to say with self.assertRaises 20 00:00:44.560 --> 00:00:46.070 and then type ValueError 21 00:00:46.070 --> 00:00:48.270 or the type of error that this is going to raise 22 00:00:48.270 --> 00:00:50.600 and in here you can write any code 23 00:00:50.600 --> 00:00:52.030 that should raise an error. 24 00:00:52.030 --> 00:00:55.180 In my case I'm gonna do divide 25 by zero. 25 00:00:55.180 --> 00:00:58.090 Now if the code in here does not raise an error, 26 00:00:58.090 --> 00:01:00.760 so if this test runs through this entire code 27 00:01:00.760 --> 00:01:02.620 which at the moment is only one line 28 00:01:02.620 --> 00:01:03.840 and there's no error raised, 29 00:01:03.840 --> 00:01:06.580 the test will fail, if there is an error raised, 30 00:01:06.580 --> 00:01:08.910 the test will pass. 31 00:01:08.910 --> 00:01:10.203 So, let's run this. 32 00:01:11.410 --> 00:01:13.830 And you can see that we have four tests ran 33 00:01:13.830 --> 00:01:16.340 and the status is okay. 34 00:01:16.340 --> 00:01:18.930 This is how I recommend checking for an error 35 00:01:18.930 --> 00:01:21.120 by using this context manager. 36 00:01:21.120 --> 00:01:25.750 However, you can also do self.assertRaises 37 00:01:25.750 --> 00:01:30.140 and in here, you can put in the exception type 38 00:01:30.140 --> 00:01:32.530 and a callable. 39 00:01:32.530 --> 00:01:35.630 That means that you could do ValueError 40 00:01:35.630 --> 00:01:38.620 and then pass in here the callable. 41 00:01:38.620 --> 00:01:40.800 Notice that if we do 25 and zero here, 42 00:01:40.800 --> 00:01:43.310 this is not a callable that we're passing. 43 00:01:43.310 --> 00:01:46.280 Here we're passing the result of calling the function 44 00:01:46.280 --> 00:01:47.420 which is not the same thing, 45 00:01:47.420 --> 00:01:50.560 so you would have to type in a lambda 46 00:01:50.560 --> 00:01:52.480 of divide 25 by zero 47 00:01:52.480 --> 00:01:53.820 or indeed, any other function 48 00:01:53.820 --> 00:01:56.084 but not call the function itself. 49 00:01:56.084 --> 00:01:58.010 So, if you do this, 50 00:01:58.010 --> 00:01:59.510 this is the same thing, 51 00:01:59.510 --> 00:02:01.270 it works in exactly the same way 52 00:02:01.270 --> 00:02:03.560 but I believe this is slightly worse 53 00:02:03.560 --> 00:02:05.840 in most circumstances. 54 00:02:05.840 --> 00:02:07.080 If you are testing a function 55 00:02:07.080 --> 00:02:09.129 that doesn't take any arguments, 56 00:02:09.129 --> 00:02:12.580 then you could do something like this 57 00:02:12.580 --> 00:02:15.400 and then this syntax here may be better 58 00:02:15.400 --> 00:02:17.070 or at least shorter. 59 00:02:17.070 --> 00:02:19.810 But because our divide function is taking arguments, 60 00:02:19.810 --> 00:02:22.290 then I find this a little bit more confusing, 61 00:02:22.290 --> 00:02:24.950 so I would go with the context manager instead 62 00:02:24.950 --> 00:02:26.573 by doing something like this. 63 00:02:27.520 --> 00:02:28.510 That's it for this video, 64 00:02:28.510 --> 00:02:31.860 this is how you check for errors being raised in your code. 65 00:02:31.860 --> 00:02:33.510 And I'll see you in the next one.