From: Russ Cox Date: Wed, 23 Jul 2014 02:56:35 +0000 (-0400) Subject: testing: add Coverage function X-Git-Tag: go1.4beta1~1029 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=d2d7187d31b46333112bab94c40308528dfa43a0;p=gostls13.git testing: add Coverage function I've found this very useful for generating good test case lists for -short mode for the disassemblers. Fixes #7959. LGTM=r R=r CC=golang-codereviews https://golang.org/cl/98150043 --- diff --git a/doc/go1.4.txt b/doc/go1.4.txt index 689c73115a..3cf595f199 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 dd29364d87..eb7249dccd 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.