Skip to content

proposal: testing: add a way to provide additional coverage information #30306

@rogpeppe

Description

@rogpeppe

The current test coverage mechanism works well when the test binary runs only once with all the tests running within that process, but sometimes it is useful to be able to run the test binary itself within the tests. For example, we may be testing a command line program that has global variables and we want to avoid inter-test pollution. The go tool uses this kind of approach, for example, by compiling itself, but it could potentially also use its own test binary.. Another example is when some code we might wish to exercise is in the form of a top level "main" function. For example, see analysis/singlechecker.

By default, although coverage information is extremely useful, it's not easy to obtain when a test suite runs the test binary as part of its tests. It is just about possible (this package does it), but only by relying on fragile parts of the system that may change in the future.

To solve this issue, I propose two new mechanisms:

  1. a way to tell the testing package to read a coverage file and add it to the total coverage
  2. a way to ask the testing package to write coverage report information even when M.Run is not called.

A possible API might look like this:

package testing

// AddCoverage adds coverage information from the given file to the total test coverage.
// The file must have been produced by the current test binary. This can
// be used to add additional coverage information written by M.WriteCoverage.
// This is a no-op if the binary was not built with coverage information enabled.
func AddCoverage(coverageFile string) error

// WriteCoverage writes coverage information to the given file.
// This is usually done automatically, but is useful if the test
// binary is re-executed as part of testing.
// This is a no-op if the binary was not built with coverage information enabled.
func (m *testing.M) WriteCoverage(file string) error

See also issue #30231

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.ProposalWaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions