tstat

package module
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2023 License: BSD-3-Clause Imports: 17 Imported by: 0

README

TStat

Tstat provides information on Go test suites and functionality to make it easier to query for information on code coverage or test cases.

For tests, it leverages the JSON output provided by go test when running with the -json flag. See here for more info.

For coverage, it leverages the cover profiles for statements and function coverage provided by the cover tool. See cover for more info.

Usage

Coverage
	stats, err := tstat.Cover("testdata/prog/cover.out", tstat.WithRootModule("github.com/nickfiggins/tstat"))
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Printf("total coverage: %#v%%\n", stats.Percent)
	pkg := stats.Packages[0]
	fmt.Printf("package: %s coverage: %#v%%\n", pkg.Name, pkg.Percent)
	fileCov := pkg.Files[0]
	for _, fn := range fileCov.Functions {
		fmt.Printf("function: %s coverage: %v%%\n", fn.Name, fn.Percent)
	}
	// Output:
	// total coverage: 25%
	// package: github.com/nickfiggins/tstat/testdata/prog coverage: 25%
	// function: add coverage: 100%
	// function: isOdd coverage: 0%
Tests
	stats, err := tstat.Tests("testdata/bigtest.json")
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Printf("test count: %v failed: %v duration: %v\n", stats.Count(), stats.Failed(), stats.Duration().String())
	pkg, _ := stats.Package("github.com/nickfiggins/tstat")
	test, _ := pkg.Test("Test_CoverageStats")
	fmt.Printf("%v/Test_CoverageStats count: %v failed: %v skipped: %v\n", test.Package, test.Count(), test.Failed(), test.Skipped())

	sub, _ := test.Test("happy") // subtest Test_CoverageStats/happy
	if !sub.Failed() {
		fmt.Printf("%v passed\n", sub.FullName)
	}
	// Output: test count: 50 failed: false duration: 473.097ms
	// github.com/nickfiggins/tstat/Test_CoverageStats count: 3 failed: false skipped: false
	// Test_CoverageStats/happy passed

Documentation

Overview

Package tstat provides utilities for parsing information from Go test runs or code coverage.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CoverOpt added in v0.0.5

type CoverOpt func(*CoverageParser)

CoverOpt is a functional option for configuring a CoverageParser.

func WithRootModule added in v0.0.5

func WithRootModule(module string) CoverOpt

WithRootModule sets the root module to trim from the file names in the coverage profile.

type Coverage

type Coverage struct {
	Percent  float64            // Percent is the total percent of statements covered.
	Packages []*PackageCoverage // Packages is the coverage of each package.
}

Coverage is the coverage statistics parsed from a single test profile.

func Cover added in v0.0.5

func Cover(coverProfile string, opts ...CoverOpt) (Coverage, error)

Cover parses the coverage and function profiles and returns a statistics based on the profiles read. The corresponding function profile will be generated automatically. If you want to provide an existing function profile, use CoverFromReaders. The cover profile must be a valid coverage profile generated by `go test -coverprofile=cover.out`.

Example
package main

import (
	"fmt"
	"log"

	"github.com/nickfiggins/tstat"
)

func main() {
	stats, err := tstat.Cover("testdata/prog/cover.out", tstat.WithRootModule("github.com/nickfiggins/tstat"))
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Printf("total coverage: %#v%%\n", stats.Percent)
	pkg := stats.Packages[0]
	fmt.Printf("package: %s coverage: %#v%%\n", pkg.Name, pkg.Percent)
	fileCov := pkg.Files[0]
	for _, fn := range fileCov.Functions {
		fmt.Printf("function: %s coverage: %v%%\n", fn.Name, fn.Percent)
	}
}
Output:

total coverage: 25%
package: github.com/nickfiggins/tstat/testdata/prog coverage: 25%
function: add coverage: 100%
function: isOdd coverage: 0%

func CoverFromReaders added in v0.0.5

func CoverFromReaders(coverProfile io.Reader, fnProfile io.Reader, opts ...CoverOpt) (Coverage, error)

CoverFromReaders parses the coverage and function profiles and returns a statistics based on the profiles read. If you want to generate the function profile automatically, use Cover instead.

func (*Coverage) Package added in v1.1.0

func (c *Coverage) Package(name string) (*PackageCoverage, bool)

Package returns the coverage of a single package in the run. It's a convenience method for finding a file in the list of file coverages.

type CoverageParser added in v0.0.5

type CoverageParser struct {
	// contains filtered or unexported fields
}

CoverageParser is a parser for coverage profiles that can be configured to read from files or io.Readers. If only a cover profile is provided, the corresponding function profile will be generated automatically. If a function profile is provided, it will be used instead of generating one - which is useful when parsing profiles that aren't part of the current project.

func NewCoverageParser added in v0.0.5

func NewCoverageParser(opts ...CoverOpt) *CoverageParser

NewCoverageParser returns a new CoverageParser with the given options.

func (*CoverageParser) Stats added in v0.0.5

func (p *CoverageParser) Stats(coverProfile, fnProfile io.Reader) (Coverage, error)

Stats parses the coverage and function profiles and returns a statistics based on the profiles read.

type FileCoverage added in v1.0.0

type FileCoverage struct {
	Name         string             // Name is the name of the file.
	Percent      float64            // Percent is the percent of statements covered.
	Functions    []FunctionCoverage // Functions is the coverage of each function in the file.
	Stmts        int                // Stmts is the total number of statements in the file.
	CoveredStmts int                // CoveredStmts is the number of statements covered in the file.
}

type FunctionCoverage added in v1.0.0

type FunctionCoverage struct {
	Name     string  // Name is the name of the function.
	Percent  float64 // Percent is the percent of statements covered.
	File     string  // File is the file the function is defined in.
	Line     int     // Line is the line the function is defined on.
	Internal bool    // Internal is true if the function is internal to the package.
}

FunctionCoverage is the coverage of a function.

type PackageCoverage added in v1.0.0

type PackageCoverage struct {
	Name    string          // Name is the name of the package.
	Percent float64         // Percent is the percentage of statements covered in the package.
	Files   []*FileCoverage // Files is the coverage of each file in the package.
}

PackageCoverage is the coverage of a package.

func (*PackageCoverage) File added in v1.1.0

func (pc *PackageCoverage) File(name string) (*FileCoverage, bool)

File returns the coverage of a file in the package. It's a convenience method for finding a file in the list of file coverages.

func (*PackageCoverage) Functions added in v1.0.0

func (pc *PackageCoverage) Functions() []FunctionCoverage

Functions returns all functions in the package.

type PackageRun added in v0.0.4

type PackageRun struct {
	Tests []*Test
	Seed  int64
	// contains filtered or unexported fields
}

PackageRun represents the results of a package test run. If the package was run with the -shuffle flag, the Seed field will be populated. Otherwise, it will be 0.

func (*PackageRun) Count added in v0.0.4

func (pr *PackageRun) Count() int

Count returns the total number of tests, including subtests.

func (*PackageRun) Duration added in v0.0.4

func (pr *PackageRun) Duration() time.Duration

Duration returns the duration of the test run.

func (*PackageRun) Failed added in v0.0.5

func (pr *PackageRun) Failed() bool

Failed returns true if any of the tests failed.

func (*PackageRun) Failures added in v0.0.6

func (pr *PackageRun) Failures() []*Test

Failures returns the tests that failed.

func (*PackageRun) Test added in v0.0.5

func (pr *PackageRun) Test(name string) (*Test, bool)

Test is a single test, which may have subtests.

type Test

type Test struct {
	Subtests []*Test // Subtests is a list of subtests for this test.

	FullName string // FullName is the full name of the test, including subtests.
	Name     string // Name is the name of the test, without the parent test name.
	Package  string // Package is the package that the test belongs to.
	// contains filtered or unexported fields
}

Test is a single test, which may have subtests.

func (*Test) Count added in v0.0.5

func (t *Test) Count() int

Count returns the total number of tests, including subtests.

func (*Test) Duration added in v1.1.2

func (t *Test) Duration() time.Duration

Duration returns the total duration of the test, including subtests.

func (*Test) Failed added in v0.0.5

func (t *Test) Failed() bool

Failed returns true if the test failed.

func (*Test) Skipped added in v0.0.6

func (t *Test) Skipped() bool

Skipped returns true if the test was skipped.

func (*Test) Test added in v0.0.5

func (t *Test) Test(name string) (*Test, bool)

Test returns the test with the given name. If the test name matches the current test, it will be returned.

type TestParser added in v0.0.5

type TestParser struct {
	// contains filtered or unexported fields
}

TestParser is a parser for test output JSON.

func NewTestParser added in v0.0.5

func NewTestParser() *TestParser

NewTestParser returns a new TestParser.

func (*TestParser) Stats added in v0.0.5

func (tp *TestParser) Stats(outJSON io.Reader) (TestRun, error)

Stats parses the test output and returns a TestRun based on the output read.

type TestRun

type TestRun struct {
	// contains filtered or unexported fields
}

TestRun represents the results of a test run, which may contain multiple packages.

func Tests added in v0.0.5

func Tests(outFile string) (TestRun, error)

Tests parses the test output JSON file and returns a TestRun based on the output read.

Example
package main

import (
	"fmt"
	"log"

	"github.com/nickfiggins/tstat"
)

func main() {
	stats, err := tstat.Tests("testdata/bigtest.json")
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Printf("test count: %v failed: %v duration: %v\n", stats.Count(), stats.Failed(), stats.Duration().String())
	pkg, _ := stats.Package("github.com/nickfiggins/tstat")
	test, _ := pkg.Test("Test_CoverageStats")
	fmt.Printf("%v/Test_CoverageStats count: %v failed: %v skipped: %v\n", test.Package, test.Count(), test.Failed(), test.Skipped())

	sub, _ := test.Test("happy") // subtest Test_CoverageStats/happy
	if !sub.Failed() {
		fmt.Printf("%v passed\n", sub.FullName)
	}
}
Output:

test count: 50 failed: false duration: 473.097ms
github.com/nickfiggins/tstat/Test_CoverageStats count: 3 failed: false skipped: false
Test_CoverageStats/happy passed

func TestsFromReader added in v0.0.5

func TestsFromReader(outJSON io.Reader) (TestRun, error)

TestsFromReader parses the test output JSON from a reader and returns a TestRun based on the output read.

func (*TestRun) Count

func (tr *TestRun) Count() int

Count returns the total number of tests, including subtests.

func (*TestRun) Duration

func (tr *TestRun) Duration() time.Duration

Duration returns the duration of the TestRun.

func (*TestRun) Failed added in v0.0.5

func (tr *TestRun) Failed() bool

Failed returns true if any of the tests failed.

func (*TestRun) Package added in v0.0.4

func (tr *TestRun) Package(name string) (PackageRun, bool)

Package returns a PackageRun for the package with the given name, if any data was recorded for it during the test run. If the package isn't found, false is returned as the second argument.

func (*TestRun) Packages added in v0.0.4

func (tr *TestRun) Packages() []PackageRun

Packages returns the packages that were run.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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