Documentation ¶
Index ¶
Examples ¶
Constants ¶
const ( ActionPass = "pass" ActionFail = "fail" )
const Usage = `` /* 532-byte string literal not displayed */
Variables ¶
var DebugWriter io.Writer = os.Stderr
DebugWriter identifies the stream to which debug information should be printed, if desired. By default it is os.Stderr.
Functions ¶
func Main ¶ added in v0.1.0
func Main() int
Main runs the command-line interface for gotestdox. The exit status for the binary is 0 if the tests passed, or 1 if the tests failed, or there was some error.
func Prettify ¶ added in v0.0.4
Prettify takes a string input representing the name of a Go test, and attempts to turn it into a readable sentence, by replacing camel-case transitions and underscores with spaces.
input is expected to be a valid Go test name, as produced by 'go test -json'. For example, input might be the string:
TestFoo/has_well-formed_output
Here, the parent test is TestFoo, and this data is about a subtest whose name is 'has well-formed output'. Go's testing package replaces spaces in subtest names with underscores, and unprintable characters with the equivalent Go literal.
Prettify does its best to reverse this transformation, yielding (something close to) the original subtest name. For example:
Foo has well-formed output
Multiword function names ¶
Because Go function names are often in camel-case, there's an ambiguity in parsing a test name like this:
TestHandleInputClosesInputAfterReading
We can see that this is about a function named HandleInput, but Prettify has no way of knowing that. Without this information, it would produce:
Handle input closes input after reading
To give it a hint, we can add an underscore after the name of the function:
TestHandleInput_ClosesInputAfterReading
This will be interpreted as marking the end of a multiword function name:
HandleInput closes input after reading
Debugging ¶
If the GOTESTDOX_DEBUG environment variable is set, Prettify will output (copious) debug information to the DebugWriter stream, elaborating on its decisions.
Example ¶
package main import ( "fmt" "github.com/bitfield/gotestdox" ) func main() { input := "TestFoo/has_well-formed_output" fmt.Println(gotestdox.Prettify(input)) }
Output: Foo has well-formed output
Example (UnderscoreHint) ¶
package main import ( "fmt" "github.com/bitfield/gotestdox" ) func main() { input := "TestHandleInput_ClosesInputAfterReading" fmt.Println(gotestdox.Prettify(input)) }
Output: HandleInput closes input after reading
Types ¶
type Event ¶
type Event struct { Action string Package string Test string Sentence string Output string Elapsed float64 }
Event represents a Go test event as recorded by the 'go test -json' command. It does not attempt to unmarshal all the data, only those fields it needs to know about. It is based on the (unexported) 'event' struct used by Go's cmd/internal/test2json package.
func ParseJSON ¶
ParseJSON takes a string representing a single JSON test record as emitted by 'go test -json', and attempts to parse it into an Event, returning any parsing error encountered.
Example ¶
package main import ( "fmt" "log" "github.com/bitfield/gotestdox" ) func main() { input := `{"Action":"pass","Package":"demo","Test":"TestItWorks","Output":"","Elapsed":0.2}` event, err := gotestdox.ParseJSON(input) if err != nil { log.Fatal(err) } fmt.Printf("%#v\n", event) }
Output: gotestdox.Event{Action:"pass", Package:"demo", Test:"TestItWorks", Sentence:"", Output:"", Elapsed:0.2}
func (Event) IsFuzzFail ¶ added in v0.2.2
func (Event) IsOutput ¶ added in v0.2.0
IsOutput determines whether or not the event is a test output (for example from testing.T.Error), excluding status messages automatically generated by 'go test' such as "--- FAIL: ..." or "=== RUN / PAUSE / CONT".
func (Event) IsPackageResult ¶ added in v0.0.9
IsPackageResult determines whether or not the test event is a package pass or fail event. That is, whether it indicates the passing or failing of a package as a whole, rather than some individual test within the package.
func (Event) IsTestResult ¶ added in v0.2.0
IsTestResult determines whether or not the test event is one that we are interested in (namely, a pass or fail event on a test). Events on non-tests (for example, examples) are ignored, and all events on tests other than pass or fail events (for example, run or pause events) are also ignored.
Example (False) ¶
package main import ( "fmt" "github.com/bitfield/gotestdox" ) func main() { event := gotestdox.Event{ Action: "fail", Test: "ExampleEventsShouldBeIgnored", } fmt.Println(event.IsTestResult()) }
Output: false
Example (True) ¶
package main import ( "fmt" "github.com/bitfield/gotestdox" ) func main() { event := gotestdox.Event{ Action: "pass", Test: "TestItWorks", } fmt.Println(event.IsTestResult()) }
Output: true
func (Event) String ¶
String formats a test Event for display. The prettified test name will be prefixed by a ✔ if the test passed, or an x if it failed.
The sentence generated by Prettify from the name of the test will be shown, followed by the elapsed time in parentheses, to 2 decimal places.
Colour ¶
If the program is attached to an interactive terminal, as determined by github.com/mattn/go-isatty, and the NO_COLOR environment variable is not set, check marks will be shown in green and x's in red.
Example ¶
package main import ( "fmt" "github.com/bitfield/gotestdox" "github.com/fatih/color" ) func main() { event := gotestdox.Event{ Action: "pass", Sentence: "It works", } color.NoColor = true fmt.Println(event.String()) }
Output: ✔ It works (0.00s)
type TestDoxer ¶ added in v0.0.5
TestDoxer holds the state and config associated with a particular invocation of 'go test'.
func NewTestDoxer ¶ added in v0.0.5
func NewTestDoxer() *TestDoxer
NewTestDoxer returns a *TestDoxer configured with the default I/O streams: os.Stdin, os.Stdout, and os.Stderr.
func (*TestDoxer) ExecGoTest ¶ added in v0.0.5
ExecGoTest runs the 'go test -json' command, with any extra args supplied by the user, and consumes its output. Any errors are reported to td's Stderr stream, including the full command line that was run. If all tests passed, td.OK will be true. If there was a test failure, or 'go test' returned some error, then td.OK will be false.
func (*TestDoxer) Filter ¶ added in v0.0.5
func (td *TestDoxer) Filter()
Filter reads from td's Stdin stream, line by line, processing JSON records emitted by 'go test -json'.
For each Go package it sees records about, it will print the full name of the package to td.Stdout, followed by a line giving the pass/fail status and the prettified name of each test, sorted alphabetically.
If all tests passed, td.OK will be true at the end. If not, or if there was a parsing error, it will be false. Errors will be reported to td.Stderr.
Example ¶
package main import ( "strings" "github.com/bitfield/gotestdox" "github.com/fatih/color" ) func main() { input := `{"Action":"pass","Package":"demo","Test":"TestItWorks"} {"Action":"pass","Package":"demo","Elapsed":0}` td := gotestdox.NewTestDoxer() td.Stdin = strings.NewReader(input) color.NoColor = true td.Filter() }
Output: demo: ✔ It works (0.00s)