You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package main
// Comment outside a functionfuncfoo() int {
// Comment inside a functioniffalse {
// Comment inside branch that is not executedreturn0
} else {
// Comment inside branch that is executed/* General comment rather than a line comment See https://go.dev/ref/spec#Comments for the difference */return1
}
}
src_test.go:
package main
import"testing"funcTestFoo(t*testing.T) {
foo()
}
go.mod:
module main
go 1.20
Next, I run the test, and generate and view an HTML coverage report:
$ go test -cover -coverprofile cp.out *.go
ok command-line-arguments 0.003s coverage: 66.7% of statements
$ go tool cover -html=cp.out
In my browser (Mozilla Firefox), this is the report I see:
Note that the comment inside the if block is considered not covered, and the comments inside the else block are considered covered. Also note the comment outside the foo function is considered not tracked.
I expect all lines that consist of only comments, or of only comments and whitespace to be not tracked.
I expect lines that only consist of comments or only consist of comments and whitespace (i.e., lines with no code) to not be tracked, as, according to the Go language specification, comments are documentation:
Comments serve as program documentation
I expect lines like
x=0// Sets x to 0
to of course count towards coverage, since it has code, but lines without code should not count towards coverage.
It does not make sense to be concerned about coverage of lines that consist of only comments, in the same way that it does not make sense to be concerned about coverage of lines that consist of only whitespace. Therefore, go test -coverprofile should consider comment-only lines as untracked.
What did you see instead?
I see comments within function bodies are covered or not covered, i.e., comments within function bodies are always tracked.
The text was updated successfully, but these errors were encountered:
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I created the following three files:
src.go:
src_test.go:
go.mod:
Next, I run the test, and generate and view an HTML coverage report:
In my browser (Mozilla Firefox), this is the report I see:
Note that the comment inside the
if
block is considered not covered, and the comments inside theelse
block are considered covered. Also note the comment outside thefoo
function is considered not tracked.The generated
cp.out
file has these contents:What did you expect to see?
I expect all lines that consist of only comments, or of only comments and whitespace to be not tracked.
I expect lines that only consist of comments or only consist of comments and whitespace (i.e., lines with no code) to not be tracked, as, according to the Go language specification, comments are documentation:
I expect lines like
to of course count towards coverage, since it has code, but lines without code should not count towards coverage.
It does not make sense to be concerned about coverage of lines that consist of only comments, in the same way that it does not make sense to be concerned about coverage of lines that consist of only whitespace. Therefore,
go test -coverprofile
should consider comment-only lines as untracked.What did you see instead?
I see comments within function bodies are covered or not covered, i.e., comments within function bodies are always tracked.
The text was updated successfully, but these errors were encountered: