summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2014-07-22 22:56:35 -0400
committerRuss Cox <rsc@golang.org>2014-07-22 22:56:35 -0400
commitf6a9612c74e164b8fdf008023bbea0040c27860e (patch)
tree23f6cb1c8ba16b3f62b48577a381e817208cf7c2
parent12ce2aeee54f37c2d4566ae36f68eb9d8ffd651d (diff)
downloadgo-f6a9612c74e164b8fdf008023bbea0040c27860e.tar.gz
testing: add Coverage function
I've found this very useful for generating good test case lists for -short mode for the disassemblers. Fixes issue 7959. LGTM=r R=r CC=golang-codereviews https://codereview.appspot.com/98150043
-rw-r--r--doc/go1.4.txt1
-rw-r--r--src/pkg/testing/cover.go23
2 files changed, 24 insertions, 0 deletions
diff --git a/doc/go1.4.txt b/doc/go1.4.txt
index 689c73115..3cf595f19 100644
--- a/doc/go1.4.txt
+++ b/doc/go1.4.txt
@@ -11,5 +11,6 @@ encoding/gob: remove unsafe (CL 102680045)
misc: deleted editor support; refer to https://code.google.com/p/go-wiki/wiki/IDEsAndTextEditorPlugins instead (CL 105470043)
runtime/race: freebsd is supported (CL 107270043)
syscall: Setuid, Setgid are disabled on linux platforms. On linux those syscalls operate on the calling thread, not the whole process. This does not match the semantics of other platforms, nor the expectations of the caller, so the operations have been disabled until issue 1435 is resolved (CL 106170043)
+testing: add Coverage (CL 98150043)
text/scanner: add IsIdentRune field of Scanner. (CL 108030044)
time: use the micro symbol (ยต (U+00B5)) to print microsecond duration (CL 105030046)
diff --git a/src/pkg/testing/cover.go b/src/pkg/testing/cover.go
index dd29364d8..eb7249dcc 100644
--- a/src/pkg/testing/cover.go
+++ b/src/pkg/testing/cover.go
@@ -34,6 +34,29 @@ type Cover struct {
CoveredPackages string
}
+// Coverage reports the current code coverage as a fraction in the range [0, 1].
+// If coverage is not enabled, Coverage returns 0.
+//
+// When running a large set of sequential test cases, checking Coverage after each one
+// can be useful for identifying which test cases exercise new code paths.
+// It is not a replacement for the reports generated by 'go test -cover' and
+// 'go tool cover'.
+func Coverage() float64 {
+ var n, d int64
+ for _, counters := range cover.Counters {
+ for _, c := range counters {
+ if c > 0 {
+ n++
+ }
+ d++
+ }
+ }
+ if d == 0 {
+ return 0
+ }
+ return float64(n) / float64(d)
+}
+
// RegisterCover records the coverage data accumulators for the tests.
// NOTE: This function is internal to the testing infrastructure and may change.
// It is not covered (yet) by the Go 1 compatibility guidelines.