WEBVTT 1 00:00:00.500 --> 00:00:02.110 Hi guys and welcome back. 2 00:00:02.110 --> 00:00:05.700 In this video, we're going to be testing our printer class. 3 00:00:05.700 --> 00:00:08.910 Let's go over to the test printer and add our input 4 00:00:09.900 --> 00:00:13.120 and then create our TestPrinter case. 5 00:00:13.120 --> 00:00:15.000 Now let's write our first test. 6 00:00:15.000 --> 00:00:19.792 So we'll say test_print_within_capacity. 7 00:00:19.792 --> 00:00:22.340 And we'll say that the printer is a new printer 8 00:00:22.340 --> 00:00:24.470 that has a certain number of pages per second, 9 00:00:24.470 --> 00:00:25.987 let's say two pages per second, 10 00:00:25.987 --> 00:00:29.983 and a capacity of 300. 11 00:00:31.470 --> 00:00:34.713 And I believe that this should be a float. 12 00:00:36.150 --> 00:00:37.060 Apologies for that, 13 00:00:37.060 --> 00:00:38.850 you do want a float there 14 00:00:38.850 --> 00:00:41.960 just in case you can print 2 1/2 pages per second. 15 00:00:41.960 --> 00:00:44.980 Now we've got our printer object here, 16 00:00:44.980 --> 00:00:48.020 and we can sway print 25 pages. 17 00:00:48.020 --> 00:00:51.040 Now there are two things that you could do here. 18 00:00:51.040 --> 00:00:53.810 You could say that the test passes 19 00:00:53.810 --> 00:00:56.053 if the correct message is returned. 20 00:00:57.040 --> 00:01:00.000 Alternatively, you could say the test fails 21 00:01:00.000 --> 00:01:01.920 if an error is raised. 22 00:01:01.920 --> 00:01:05.360 So if you think the test should pass if there is no error, 23 00:01:05.360 --> 00:01:08.000 then this is an sufficient test. 24 00:01:08.000 --> 00:01:11.880 Because if you run it, you'll see that no error is raised 25 00:01:11.880 --> 00:01:13.210 and therefore the test passes. 26 00:01:13.210 --> 00:01:16.493 All tests pass by default unless there is an error in them. 27 00:01:17.830 --> 00:01:22.490 Alternatively, you could say that the message is that, 28 00:01:22.490 --> 00:01:26.550 and then you could say self.assertEqual message, 29 00:01:26.550 --> 00:01:29.160 or rather, printed 30 00:01:31.240 --> 00:01:35.740 25 pages in 12.5 seconds 31 00:01:37.310 --> 00:01:39.143 is equal to the message. 32 00:01:40.310 --> 00:01:42.530 Oh, that should be 12.50 seconds 33 00:01:42.530 --> 00:01:44.930 because we formatted it to two decimal places. 34 00:01:44.930 --> 00:01:47.470 And I have a error here. 35 00:01:47.470 --> 00:01:50.600 So you can see how writing this message out is actually 36 00:01:50.600 --> 00:01:52.060 potentially a helpful thing. 37 00:01:52.060 --> 00:01:54.230 There you have your test passing. 38 00:01:54.230 --> 00:01:56.850 Either of these is correct. 39 00:01:56.850 --> 00:02:00.480 I personally would not check for the message. 40 00:02:00.480 --> 00:02:02.510 I would have a separate test for that. 41 00:02:02.510 --> 00:02:05.140 I would just check that the printer prints. 42 00:02:05.140 --> 00:02:07.700 Now for all our tests that we'll be writing, 43 00:02:07.700 --> 00:02:10.340 we'll be using a printer object. 44 00:02:10.340 --> 00:02:12.620 So in order to reduce duplication, 45 00:02:12.620 --> 00:02:16.370 we can extract this and put it into a separate method 46 00:02:16.370 --> 00:02:19.270 that we'll call the setUp method. 47 00:02:19.270 --> 00:02:22.940 And you don't need this return none, that is implied. 48 00:02:22.940 --> 00:02:26.070 So we'll say self.printer is a printer 49 00:02:26.070 --> 00:02:27.670 where the pages per second is 2.0 50 00:02:27.670 --> 00:02:29.493 and the capacity is 300. 51 00:02:32.953 --> 00:02:35.350 And then what that means is you no longer need that, 52 00:02:35.350 --> 00:02:38.160 but you do need to use self.printer. 53 00:02:38.160 --> 00:02:42.660 Now this setUp method here runs before each test. 54 00:02:42.660 --> 00:02:44.790 So if you have multiple test methods, 55 00:02:44.790 --> 00:02:46.470 this one will run before each one. 56 00:02:46.470 --> 00:02:49.880 So you always get a new printer every test. 57 00:02:49.880 --> 00:02:52.230 If you want to run this method only once 58 00:02:52.230 --> 00:02:54.300 for the entire test, 59 00:02:54.300 --> 00:02:56.123 then you will do setUpClass, 60 00:02:57.378 --> 00:03:00.420 ad you will get rid of this self here, 61 00:03:00.420 --> 00:03:04.280 and you could do cls.printer is this printer. 62 00:03:04.280 --> 00:03:07.140 And of course you do need to make this a class method. 63 00:03:07.140 --> 00:03:09.330 And here you can still access the self.printer 64 00:03:09.330 --> 00:03:11.800 and it will know that you refer to cls.printer 65 00:03:11.800 --> 00:03:14.480 just because of how Python's inheritance works. 66 00:03:14.480 --> 00:03:16.950 You would choose one or the other purely depending 67 00:03:16.950 --> 00:03:19.900 on whether you want a new printer every test, 68 00:03:19.900 --> 00:03:23.950 or you want to reuse the same printer for the tests. 69 00:03:23.950 --> 00:03:28.950 Because the printer has a state that varies over time, 70 00:03:29.190 --> 00:03:31.750 and that is this capacity here, 71 00:03:31.750 --> 00:03:33.980 I strongly recommend 72 00:03:33.980 --> 00:03:37.680 that you do not share the printer across tests. 73 00:03:37.680 --> 00:03:42.083 So I would make this into a normal setUp method like that. 74 00:03:43.110 --> 00:03:46.730 The reason for that is because if you write a test 75 00:03:46.730 --> 00:03:49.363 that uses up all of the capacity, 76 00:03:50.920 --> 00:03:52.760 then you have another test 77 00:03:52.760 --> 00:03:54.160 that tries to print some more, 78 00:03:54.160 --> 00:03:57.340 but you've run out of capacity so the test would fail. 79 00:03:57.340 --> 00:04:00.690 if you have one printer for all tests. 80 00:04:00.690 --> 00:04:03.010 By having a printer for each test, 81 00:04:03.010 --> 00:04:04.480 you don't have that problem 82 00:04:04.480 --> 00:04:06.833 because you always get a new one for each test. 83 00:04:07.680 --> 00:04:12.330 So whenever you have state that changes in the object, 84 00:04:12.330 --> 00:04:14.220 create a new object for each test. 85 00:04:14.220 --> 00:04:17.210 Do not share objects across tests. 86 00:04:17.210 --> 00:04:18.520 All right, I just wanted to show you 87 00:04:18.520 --> 00:04:20.800 about the setUp and setUpClass, 88 00:04:20.800 --> 00:04:22.430 and also I wanted to tell you 89 00:04:22.430 --> 00:04:25.760 that there is a tearDown and tearDownClass. 90 00:04:25.760 --> 00:04:28.930 So you've got tearDown and tearDownClass, 91 00:04:28.930 --> 00:04:30.220 these two methods here, 92 00:04:30.220 --> 00:04:32.830 in case you wanna run something after each test. 93 00:04:32.830 --> 00:04:34.750 That you can do of course. 94 00:04:34.750 --> 00:04:35.940 That's everything for this video. 95 00:04:35.940 --> 00:04:37.400 Thanks for joining me in this one. 96 00:04:37.400 --> 00:04:38.340 I'll see you in the next one, 97 00:04:38.340 --> 00:04:39.870 where we're gonna continue with some of this. 98 00:04:39.870 --> 00:04:40.820 I'll see you there.