1 00:00:06,670 --> 00:00:08,600 - I wanna show you how the built-in 2 00:00:08,600 --> 00:00:11,390 code coverage tooling works in Go. 3 00:00:11,390 --> 00:00:13,510 This is really brilliant stuff. 4 00:00:13,510 --> 00:00:15,700 In one of the earlier sections, I told you, 5 00:00:15,700 --> 00:00:18,300 how do you know when you're done with a piece of code? 6 00:00:18,300 --> 00:00:20,027 And one of the things we said was code coverage. 7 00:00:20,027 --> 00:00:21,920 And I believe that you need to be somewhere 8 00:00:21,920 --> 00:00:25,510 between 70 and 80% of code coverage overall 9 00:00:25,510 --> 00:00:27,140 before you tell me that you're done. 10 00:00:27,140 --> 00:00:29,640 I do wanna see 100% on the happy path, 11 00:00:29,640 --> 00:00:31,260 but I want that 70 or 80%. 12 00:00:31,260 --> 00:00:32,960 But how can I check that? 13 00:00:32,960 --> 00:00:34,540 Let me show you how it works. 14 00:00:34,540 --> 00:00:36,790 Now, I'm back into my kit project here, 15 00:00:36,790 --> 00:00:39,260 and I'm using my TCP package here 16 00:00:39,260 --> 00:00:42,040 to show you how we can do code coverage. 17 00:00:42,040 --> 00:00:46,280 Notice I've got two files here for _test 18 00:00:46,280 --> 00:00:48,890 that's allowing me to test this API, 19 00:00:48,890 --> 00:00:51,010 and the API is over three files here. 20 00:00:51,010 --> 00:00:55,470 It's client.go, tcp.go and tcpconfig. 21 00:00:55,470 --> 00:00:58,640 So we could do some basic code coverage 22 00:00:58,640 --> 00:01:00,580 by just moving into that folder. 23 00:01:00,580 --> 00:01:03,410 There I am already, I'm in our TCP folder. 24 00:01:03,410 --> 00:01:05,510 And I could do some very basic code coverage 25 00:01:05,510 --> 00:01:10,330 by just saying go test and just adding the word -cover, 26 00:01:10,330 --> 00:01:13,620 so using the cover flag off the tool. 27 00:01:13,620 --> 00:01:15,720 Now, if you notice, what this is saying, 28 00:01:15,720 --> 00:01:19,560 I'm only using 58.3%, right, of code coverage, 29 00:01:19,560 --> 00:01:21,360 which is bad, because I already told you 30 00:01:21,360 --> 00:01:23,050 that if you're not at 70%, 31 00:01:23,050 --> 00:01:25,140 you can't tell me that you're done. 32 00:01:25,140 --> 00:01:26,750 So maybe you shouldn't listen to me anymore 33 00:01:26,750 --> 00:01:28,310 because I've got this code out there 34 00:01:28,310 --> 00:01:30,567 that doesn't have the proper code coverage. 35 00:01:30,567 --> 00:01:33,500 And this stuff happens because sometimes you're moving fast 36 00:01:33,500 --> 00:01:35,200 to add feature functionality 37 00:01:35,200 --> 00:01:36,310 and you say you're gonna go back 38 00:01:36,310 --> 00:01:38,210 and add the test code coverage later. 39 00:01:38,210 --> 00:01:40,350 This is why we need those checks and balances. 40 00:01:40,350 --> 00:01:42,350 Okay, that's great, 58%, 41 00:01:42,350 --> 00:01:46,053 but we don't really know where that 58% is. 42 00:01:47,250 --> 00:01:49,050 What we need is a cover profile. 43 00:01:49,050 --> 00:01:50,980 If we generate a cover profile, 44 00:01:50,980 --> 00:01:53,010 we'll know where that information is. 45 00:01:53,010 --> 00:01:55,010 So instead of just saying cover, 46 00:01:55,010 --> 00:01:57,800 I'm gonna say, give me a cover profile 47 00:01:57,800 --> 00:02:00,410 and let's write that cover profile, 48 00:02:00,410 --> 00:02:02,083 let's say to c.out. 49 00:02:03,550 --> 00:02:08,230 What I now have done is I've generated a c.out file 50 00:02:08,230 --> 00:02:11,810 with the profiling information for the cover report. 51 00:02:11,810 --> 00:02:13,950 But how do I look at this coverage? 52 00:02:13,950 --> 00:02:16,780 Well, go tool cover, 53 00:02:16,780 --> 00:02:17,940 there's lots of go tools, 54 00:02:17,940 --> 00:02:19,900 we're gonna see pprof and tracing soon, 55 00:02:19,900 --> 00:02:21,910 but this is go tool cover. 56 00:02:21,910 --> 00:02:26,910 Now I wanna get an HTML representation in the browser, 57 00:02:27,210 --> 00:02:29,440 so I'm gonna say, use html, 58 00:02:29,440 --> 00:02:33,210 and here is the cover profile, c.out. 59 00:02:33,210 --> 00:02:35,700 That's all I gotta do, html c.out. 60 00:02:35,700 --> 00:02:36,970 Look at what happens. 61 00:02:36,970 --> 00:02:39,530 Yep, a browser window opens up 62 00:02:39,530 --> 00:02:42,540 and I'm starting to see the code against the coverage. 63 00:02:42,540 --> 00:02:44,520 And I've got a dropdown here 64 00:02:44,520 --> 00:02:47,850 that shows me at the file level what we're looking at. 65 00:02:47,850 --> 00:02:51,210 Now what I'm looking at is client.go, 66 00:02:51,210 --> 00:02:53,410 which is giving me, 67 00:02:53,410 --> 00:02:58,070 right now, 93% code coverage. 68 00:02:58,070 --> 00:03:02,730 I can look at tcp.go, it's giving me 52%, 69 00:03:02,730 --> 00:03:04,920 and 53% on config. 70 00:03:04,920 --> 00:03:07,050 Let's look on tcp.go, this is horrible. 71 00:03:07,050 --> 00:03:09,030 This should be at at least 70%. 72 00:03:09,030 --> 00:03:11,620 Let's see why we're not at 70%. 73 00:03:11,620 --> 00:03:12,930 And I can do a scroll down 74 00:03:12,930 --> 00:03:14,740 and immediately I see this. 75 00:03:14,740 --> 00:03:17,230 This is the implementation of the error interface 76 00:03:17,230 --> 00:03:18,910 for this client error type. 77 00:03:18,910 --> 00:03:23,300 I have no tests that validate this code, not good. 78 00:03:23,300 --> 00:03:25,860 Now I've got some really nice code coverage for new, 79 00:03:25,860 --> 00:03:27,070 everything is green. 80 00:03:27,070 --> 00:03:29,750 And remember, I wanna test that I have 100% code coverage 81 00:03:29,750 --> 00:03:30,960 on the happy path. 82 00:03:30,960 --> 00:03:33,610 This is how I can do that by looking at the green. 83 00:03:33,610 --> 00:03:37,270 These are edge cases I'll probably never run, that's okay. 84 00:03:37,270 --> 00:03:38,930 Look at all this code here. 85 00:03:38,930 --> 00:03:42,510 These are kinda edge cases that I might never be able to run 86 00:03:42,510 --> 00:03:45,130 because they're dealing with socket failures. 87 00:03:45,130 --> 00:03:46,360 Here we go. 88 00:03:46,360 --> 00:03:49,150 And this is completely unacceptable. 89 00:03:49,150 --> 00:03:52,400 And if I'm a manager and you came to me with this 90 00:03:52,400 --> 00:03:53,800 and you said you were done, 91 00:03:53,800 --> 00:03:56,650 you and I, I'm gonna send you back to your desk, right? 92 00:03:56,650 --> 00:03:59,310 You've got an enter API here call done 93 00:03:59,310 --> 00:04:02,560 and you haven't written a single test for it. 94 00:04:02,560 --> 00:04:03,393 Not good. 95 00:04:03,393 --> 00:04:04,530 But guess what, there it is. 96 00:04:04,530 --> 00:04:06,620 And this almost becomes a game now 97 00:04:06,620 --> 00:04:10,410 because we can add tests and just watch the stuff go green, 98 00:04:10,410 --> 00:04:13,040 go green, go green, and keep running it. 99 00:04:13,040 --> 00:04:15,930 So it's really nice that Go gives us the ability 100 00:04:15,930 --> 00:04:19,100 to look at code coverage using the dash cover command, 101 00:04:19,100 --> 00:04:21,170 or we can come back 102 00:04:21,170 --> 00:04:26,110 and use this cover profile flag 103 00:04:26,110 --> 00:04:29,070 and then use the Go tool cover with the html 104 00:04:29,070 --> 00:04:31,760 to view all of our code coverage here. 105 00:04:31,760 --> 00:04:32,870 And I really want you to make sure 106 00:04:32,870 --> 00:04:35,770 you're at that 70 to 80% code coverage. 107 00:04:35,770 --> 00:04:39,070 Use this tooling to validate what is really missing 108 00:04:39,070 --> 00:04:41,280 and what you could add, you should be adding, 109 00:04:41,280 --> 00:04:43,780 before you go and tell everybody that you're done.