- I wanna show you how the built-in
code coverage tooling works in Go.
This is really brilliant stuff.
In one of the earlier sections, I told you,
how do you know when you're done with a piece of code?
And one of the things we said was code coverage.
And I believe that you need to be somewhere
between 70 and 80% of code coverage overall
before you tell me that you're done.
I do wanna see 100% on the happy path,
but I want that 70 or 80%.
But how can I check that?
Let me show you how it works.
Now, I'm back into my kit project here,
and I'm using my TCP package here
to show you how we can do code coverage.
Notice I've got two files here for _test
that's allowing me to test this API,
and the API is over three files here.
It's client.go, tcp.go and tcpconfig.
So we could do some basic code coverage
by just moving into that folder.
There I am already, I'm in our TCP folder.
And I could do some very basic code coverage
by just saying go test and just adding the word -cover,
so using the cover flag off the tool.
Now, if you notice, what this is saying,
I'm only using 58.3%, right, of code coverage,
which is bad, because I already told you
that if you're not at 70%,
you can't tell me that you're done.
So maybe you shouldn't listen to me anymore
because I've got this code out there
that doesn't have the proper code coverage.
And this stuff happens because sometimes you're moving fast
to add feature functionality
and you say you're gonna go back
and add the test code coverage later.
This is why we need those checks and balances.
Okay, that's great, 58%,
but we don't really know where that 58% is.
What we need is a cover profile.
If we generate a cover profile,
we'll know where that information is.
So instead of just saying cover,
I'm gonna say, give me a cover profile
and let's write that cover profile,
let's say to c.out.
What I now have done is I've generated a c.out file
with the profiling information for the cover report.
But how do I look at this coverage?
Well, go tool cover,
there's lots of go tools,
we're gonna see pprof and tracing soon,
but this is go tool cover.
Now I wanna get an HTML representation in the browser,
so I'm gonna say, use html,
and here is the cover profile, c.out.
That's all I gotta do, html c.out.
Look at what happens.
Yep, a browser window opens up
and I'm starting to see the code against the coverage.
And I've got a dropdown here
that shows me at the file level what we're looking at.
Now what I'm looking at is client.go,
which is giving me,
right now, 93% code coverage.
I can look at tcp.go, it's giving me 52%,
and 53% on config.
Let's look on tcp.go, this is horrible.
This should be at at least 70%.
Let's see why we're not at 70%.
And I can do a scroll down
and immediately I see this.
This is the implementation of the error interface
for this client error type.
I have no tests that validate this code, not good.
Now I've got some really nice code coverage for new,
everything is green.
And remember, I wanna test that I have 100% code coverage
on the happy path.
This is how I can do that by looking at the green.
These are edge cases I'll probably never run, that's okay.
Look at all this code here.
These are kinda edge cases that I might never be able to run
because they're dealing with socket failures.
Here we go.
And this is completely unacceptable.
And if I'm a manager and you came to me with this
and you said you were done,
you and I, I'm gonna send you back to your desk, right?
You've got an enter API here call done
and you haven't written a single test for it.
Not good.
But guess what, there it is.
And this almost becomes a game now
because we can add tests and just watch the stuff go green,
go green, go green, and keep running it.
So it's really nice that Go gives us the ability
to look at code coverage using the dash cover command,
or we can come back
and use this cover profile flag
and then use the Go tool cover with the html
to view all of our code coverage here.
And I really want you to make sure
you're at that 70 to 80% code coverage.
Use this tooling to validate what is really missing
and what you could add, you should be adding,
before you go and tell everybody that you're done.