gotest

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package gotest counts the results in go test -json output so that a skipped test never reads as a passed one.

Count reads the test2json stream of one go test run together with the run's exit error. It keeps every test's outcome by name, including the output a skipped test printed, and counts as failures the things a plain pass/fail tally misses: a package that fails with no failing test (a build failure or a panic in TestMain), a test that starts and never finishes (a timeout), output that is not test2json, and a nonzero exit with no failure event at all. Report.Err is nil only if nothing failed and at least one test passed.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/tmc/lib/nvidia/internal/gotest"
)

func main() {
	const out = `{"Action":"run","Package":"p","Test":"TestA"}
{"Action":"pass","Package":"p","Test":"TestA"}
{"Action":"run","Package":"p","Test":"TestGPU"}
{"Action":"output","Package":"p","Test":"TestGPU","Output":"    no CUDA device\n"}
{"Action":"skip","Package":"p","Test":"TestGPU"}
{"Action":"pass","Package":"p"}`
	r, _ := gotest.Count(strings.NewReader(out), nil)
	fmt.Printf("passed=%d skipped=%d failed=%d err=%v\n", r.Passed, r.Skipped, r.Failed, r.Err())
	for _, t := range r.With(gotest.Skip) {
		fmt.Printf("skipped %s: %s", t.Name, strings.TrimSpace(t.Output))
	}
}
Output:
passed=1 skipped=1 failed=0 err=<nil>
skipped TestGPU: no CUDA device

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Package

type Package struct {
	Path   string
	Status Status
	Output string // output not attributed to a test
}

A Package is one package's own outcome. Skip means it has no tests.

type Report

type Report struct {
	Tests    []Test // in the order they finished; unfinished ones last
	Packages []Package

	Passed  int // tests that passed
	Skipped int // tests that skipped; never counted as passed
	// Failed counts failed and unfinished tests plus one for each
	// Problem.
	Failed int
	// Problems are failures not attributed to a test: a package that
	// failed with no failing test, output that is not test2json, and a
	// nonzero exit with no failure event.
	Problems []string
}

A Report is the outcome of one go test run.

func Count

func Count(r io.Reader, runErr error) (*Report, error)

Count reads go test -json output from r. runErr is the error go test exited with, if any. Count returns an error only if r cannot be read.

func (*Report) Err

func (r *Report) Err() error

Err reports why the run is not a pass: something failed, or no test passed. A run whose tests all skipped is not a pass.

func (*Report) With

func (r *Report) With(s Status) []Test

With returns the tests with status s, in report order.

type Status

type Status string

A Status is the outcome of a test or package.

const (
	Pass Status = "pass"
	Fail Status = "fail"
	Skip Status = "skip"
)

type Test

type Test struct {
	Package string
	Name    string
	Status  Status // Fail if the test never finished
	Elapsed float64
	Output  string // what the test printed, including a skip reason
}

A Test is one test, subtest, example or fuzz target that started.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL