WEBVTT 1 00:00:00.350 --> 00:00:04.070 Hi, guys and welcome to this section on unit testing. 2 00:00:04.070 --> 00:00:05.440 In this section, we're going to look 3 00:00:05.440 --> 00:00:08.440 at what tests are, why you should do them 4 00:00:08.440 --> 00:00:11.030 and also how you test with Python 5 00:00:11.030 --> 00:00:14.053 using the built-in library called Unit Test. 6 00:00:15.060 --> 00:00:18.220 Testing has two main purposes. 7 00:00:18.220 --> 00:00:20.780 The first one is to describe 8 00:00:20.780 --> 00:00:21.980 what your application does 9 00:00:21.980 --> 00:00:24.510 and what your functions in your application do. 10 00:00:24.510 --> 00:00:26.710 And the second one is to keep you right 11 00:00:26.710 --> 00:00:29.090 as your application changes. 12 00:00:29.090 --> 00:00:30.660 So, you can think of tests 13 00:00:30.660 --> 00:00:33.990 as checks and there's a bit of a discussion 14 00:00:33.990 --> 00:00:37.030 around the testing community with these names, 15 00:00:37.030 --> 00:00:40.160 whether tests should be called checks or not. 16 00:00:40.160 --> 00:00:44.260 A check is something that you run on your application code 17 00:00:44.260 --> 00:00:46.220 and always checks the same thing, 18 00:00:46.220 --> 00:00:49.070 for example, that a function gives you the right value 19 00:00:49.070 --> 00:00:50.200 for certain inputs 20 00:00:51.200 --> 00:00:53.940 whereas a test, some people like to say 21 00:00:53.940 --> 00:00:56.860 is when you go and you manually try 22 00:00:56.860 --> 00:00:58.180 to break your application, 23 00:00:58.180 --> 00:01:00.530 you try to verify that's it's doing what it has to do, 24 00:01:00.530 --> 00:01:02.770 you're trying to verify that it doesn't have any problems 25 00:01:02.770 --> 00:01:05.510 that you haven't noticed before and so on. 26 00:01:05.510 --> 00:01:07.930 After you test and you find any problems, 27 00:01:07.930 --> 00:01:10.060 you then write checks for those 28 00:01:10.060 --> 00:01:11.410 so that they don't happen again 29 00:01:11.410 --> 00:01:13.820 when you change your code again. 30 00:01:13.820 --> 00:01:14.800 Let me say at this point 31 00:01:14.800 --> 00:01:17.280 that if your application is very small 32 00:01:17.280 --> 00:01:19.150 and you don't think it's every going to grow, 33 00:01:19.150 --> 00:01:21.240 for example, if you're making sample apps 34 00:01:21.240 --> 00:01:23.610 for a portfolio or something like that, 35 00:01:23.610 --> 00:01:25.600 they probably don't need tests 36 00:01:25.600 --> 00:01:27.910 just because you're not gonna be changing the code 37 00:01:27.910 --> 00:01:30.260 which means that there's nothing for you to check 38 00:01:30.260 --> 00:01:32.120 as your code changes. 39 00:01:32.120 --> 00:01:33.860 Once your application is working, 40 00:01:33.860 --> 00:01:36.110 there's no need to check anything long term 41 00:01:36.110 --> 00:01:38.170 because there's nothing that changes. 42 00:01:38.170 --> 00:01:41.210 However, writing tests is a key software skill, 43 00:01:41.210 --> 00:01:43.880 as usual when you're working on software projects, 44 00:01:43.880 --> 00:01:45.990 the projects keep growing and growing constantly, 45 00:01:45.990 --> 00:01:48.640 so having tests there is a good thing, 46 00:01:48.640 --> 00:01:50.240 so maybe you do wanna write tests 47 00:01:50.240 --> 00:01:52.110 for your portfolio apps anyway 48 00:01:52.110 --> 00:01:54.660 just so your potential employers know that you can. 49 00:01:55.640 --> 00:02:00.110 With that said, tests also have a final purpose 50 00:02:00.110 --> 00:02:03.360 which is that if you can easily write tests 51 00:02:03.360 --> 00:02:07.240 for your code, that means your code is easily testable 52 00:02:07.240 --> 00:02:09.720 and usually almost always, 53 00:02:09.720 --> 00:02:13.310 easily testable code has a high correlation 54 00:02:13.310 --> 00:02:15.337 to good-quality code 55 00:02:15.337 --> 00:02:18.040 just because if it's easy to test, 56 00:02:18.040 --> 00:02:19.660 it means it's simple to think about 57 00:02:19.660 --> 00:02:20.785 and to reason about 58 00:02:20.785 --> 00:02:23.614 and that's why it's easy to test. 59 00:02:23.614 --> 00:02:26.270 If writing tests is very difficult, 60 00:02:26.270 --> 00:02:28.610 that means that probably your code is too complicated 61 00:02:28.610 --> 00:02:30.560 or you don't fully understand it. 62 00:02:30.560 --> 00:02:34.130 And that can be a sign that something's wrong with the code. 63 00:02:34.130 --> 00:02:35.670 So, those three purposes 64 00:02:35.670 --> 00:02:38.086 to describe what the code does, 65 00:02:38.086 --> 00:02:41.090 to check long term as the code changes 66 00:02:41.090 --> 00:02:43.290 and to help you write better code 67 00:02:43.290 --> 00:02:46.210 are the main purpose of testing. 68 00:02:46.210 --> 00:02:47.900 So, in this section, we're going to look 69 00:02:47.900 --> 00:02:51.150 at how you do it, so join me in the next video, 70 00:02:51.150 --> 00:02:52.100 I'll see you there.