WEBVTT 00:00.490 --> 00:04.760 We are now at the seventh video of this section testing and. 00:04.760 --> 00:12.760 D d That is test driven development in the previous video we looked at pointers and structures in this 00:12.760 --> 00:13.240 video. 00:13.270 --> 00:21.280 We will learn about the testing package and see what is TDD go has a powerful testing package that allows 00:21.280 --> 00:25.470 you also to work in a TDD environment quite easily. 00:25.480 --> 00:31.390 It is also very convenient to check the portions of your code without the need to write an entire main 00:31.390 --> 00:33.510 application that uses it. 00:33.520 --> 00:36.430 Now let's have a look at the testing package. 00:36.580 --> 00:40.400 So testing is very important in every programming language. 00:40.540 --> 00:46.960 Go creators knew it and decided to provide all libraries and packages needed for the test in the core 00:46.960 --> 00:47.980 package. 00:48.010 --> 00:54.220 You don't need any third party library for testing or code coverage the package that allows for testing 00:54.220 --> 00:57.850 go apps is called conveniently testing. 00:57.850 --> 01:03.490 We will create a small app that sums two numbers that we provide through the command line with our main 01:03.490 --> 01:04.380 dot go. 01:04.390 --> 01:08.840 We have added these lines of code that computes the sum of two numbers. 01:09.510 --> 01:17.230 Let us execute our program in the terminal to get the some go run main dot go and we give the two numbers 01:17.290 --> 01:19.280 as 3 and four. 01:19.450 --> 01:24.780 So as you can see we got the output as the sum of three and or is seven. 01:24.850 --> 01:30.020 By the way we are using the SDR conf package to convert strings to other types. 01:30.070 --> 01:38.230 In this case to end the method 80 0 I received a string and returns an int and an error that for simplicity 01:38.440 --> 01:41.560 we are ignoring here by using the underscore. 01:41.740 --> 01:47.380 You can ignore variable returns by using the underscores if necessary but usually you don't want to 01:47.380 --> 01:49.150 ignore errors. 01:49.160 --> 01:55.120 Okay so let's write a test that checks the correct result of the sum we have created a new file called 01:55.270 --> 02:02.410 Main underscore test dot go by convention test files are named like the files they're testing plus the 02:02.410 --> 02:05.700 underscore test suffix within this file. 02:05.740 --> 02:07.610 Add these lines of code. 02:07.840 --> 02:11.720 Make sure that the package used in both is the same. 02:11.770 --> 02:18.390 I have used the main package as you can see testing in Go is used by writing methods started with the 02:18.390 --> 02:27.450 prefix test a test name and the injection of the testing dot t pointer called T contrary to other languages. 02:27.450 --> 02:34.500 There are no asserts no special syntax for testing in go you can use go syntax to check for errors and 02:34.500 --> 02:38.620 you call T with information about the error in case it fails. 02:38.670 --> 02:44.070 If the code reaches the end of the test function without arising errors the function has passed the 02:44.070 --> 02:46.810 tests to run a test and go. 02:46.860 --> 02:55.080 You must use the go test hyphen v command hyphen V is to receive verbose output from the test keyword. 02:55.080 --> 02:56.220 Let me show you. 02:56.370 --> 02:58.060 Open a terminal and write. 02:58.080 --> 03:00.140 Go test hyphen V. 03:00.390 --> 03:02.230 So you can see the output. 03:02.460 --> 03:04.360 Our tests were correct. 03:04.360 --> 03:10.680 Let's see what happens if we break things on purpose and we change the expected value in main underscore. 03:10.700 --> 03:14.160 Test dot go from eleven to ten. 03:14.160 --> 03:16.770 Let's save this file and open the terminal. 03:16.770 --> 03:18.090 Execute the command. 03:18.090 --> 03:21.390 Go test as you can see over here. 03:21.450 --> 03:27.840 The test has failed as we expected the testing package provides the information you set on the test. 03:27.870 --> 03:29.700 Let's make it work again and check. 03:29.700 --> 03:36.130 Test coverage change the value of the variable expected from 10 to 11 again and run the command. 03:36.140 --> 03:39.120 Go test iPhone cover to see the code. 03:39.120 --> 03:45.400 Coverage the hyphen cover option gives us information about the code coverage for a given package. 03:45.410 --> 03:52.060 Unfortunately it doesn't provide information about overall application coverage before we move further 03:52.210 --> 03:59.740 let us see what TDD is TDD is the acronym for test driven development. 03:59.740 --> 04:03.800 It consists of writing the tests first before writing the function. 04:03.940 --> 04:09.530 The TDD changes the way to write code and structure code so that it can be tested. 04:09.550 --> 04:10.920 So how does it work. 04:10.930 --> 04:13.820 Let's explain this with a real life example. 04:13.990 --> 04:18.090 Imagine that you are in summer and you want to be refreshed somehow. 04:18.130 --> 04:22.320 You can build a pool it with cold water and jump into it. 04:22.420 --> 04:26.780 But in TDD terms the steps will be as listed here. 04:26.920 --> 04:30.460 First you jump into a place where the pool will be built. 04:30.550 --> 04:35.140 That is you write a test that you know it will fail next. 04:35.530 --> 04:37.540 It hurts and you aren't cool either. 04:37.600 --> 04:41.150 Which means yes the test failed as we predicted. 04:41.440 --> 04:45.050 After this you build a pool and fill it with cold water. 04:45.190 --> 04:49.350 So you code the functionality and then you jump into the pool. 04:49.420 --> 04:52.500 Or rather you repeat the point one test again. 04:52.720 --> 04:59.480 Following this your code now also objects completed which means test passed. 04:59.680 --> 05:02.930 Finally go to the fridge and take a beer to the pool drink. 05:03.100 --> 05:09.740 Double also nurse or we could say refactor the code so let's repeat the previous example. 05:09.790 --> 05:15.640 But with a multiplication First we will write the Declaration of the function that we're going to test. 05:15.970 --> 05:17.950 Let's save this file. 05:18.010 --> 05:22.530 Now let's write the test that will check the correctness of the previous function. 05:22.900 --> 05:25.640 And now we test it through the command line. 05:25.660 --> 05:29.980 So first save this file and open the command line terminal here. 05:30.190 --> 05:31.540 Execute the command. 05:31.540 --> 05:37.180 Go test nice like in our pool example where the water wasn't there yet. 05:37.220 --> 05:40.430 Our function returns an incorrect value too. 05:40.480 --> 05:46.330 So now we have a function declaration but isn't defined yet and the test that fails. 05:46.460 --> 05:48.970 Let's go back to main dot go file. 05:48.970 --> 05:51.280 Let's add these lines of code. 05:51.310 --> 05:57.910 Now we have to make the test pass by writing the function and executing the test to check. 05:57.910 --> 06:02.740 Let's go back to the terminal and we execute again our testing suite. 06:02.750 --> 06:09.920 After writing our code correctly the test should pass so we can continue to the refactoring process. 06:10.050 --> 06:10.790 Great. 06:10.800 --> 06:14.750 We have developed the multiply function following TDD. 06:14.790 --> 06:19.820 Now we must refactor our code but we cannot make it more simple or readable. 06:19.860 --> 06:26.850 So the loop can be considered closed during this course we will write many tests that define the functionality 06:26.850 --> 06:34.380 that we want to achieve in our patterns TDD promotes encapsulation and abstraction just like design 06:34.380 --> 06:37.130 patterns to in this video. 06:37.200 --> 06:42.990 We have learned about testing and test driven development or TDD in the next video. 06:43.050 --> 06:46.080 We will dive into the libraries available in go.