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
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
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.
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.
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
PackageRun represents the results of a package test run. If the package was run with the -shuffle flag,.
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.
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.
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.
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
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.Println(stats.Count(), stats.Failed(), stats.Duration().String()) pkg, _ := stats.Package("github.com/nickfiggins/tstat") test, _ := pkg.Test("Test_CoverageStats") fmt.Println(test.Count(), test.Failed(), test.Skipped(), test.Package) sub, _ := test.Test("happy") // subtest Test_CoverageStats/happy if !sub.Failed() { fmt.Printf("%v passed\n", sub.FullName) } }
Output: 50 false 473.097ms 3 false false github.com/nickfiggins/tstat Test_CoverageStats/happy passed
func TestsFromReader ¶ added in v0.0.5
TestsFromReader parses the test output JSON from a reader and returns a TestRun based on the output read.
func (*TestRun) Package ¶ added in v0.0.4
func (tr *TestRun) Package(name string) (PackageRun, bool)
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 (*TestRun) Packages ¶ added in v0.0.4
func (tr *TestRun) Packages() []PackageRun
Packages returns the packages that were run.