Skip to content

Commit

Permalink
Add --max-fails flag
Browse files Browse the repository at this point in the history
To terminate the test run when the number of failures is reached.
  • Loading branch information
dnephin committed Oct 31, 2020
1 parent b081783 commit 3d0c6fa
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type eventHandler struct {
formatter testjson.EventFormatter
err io.Writer
jsonFile io.WriteCloser
maxFails int
}

func (h *eventHandler) Err(text string) error {
Expand All @@ -37,6 +38,10 @@ func (h *eventHandler) Event(event testjson.TestEvent, execution *testjson.Execu
if err != nil {
return errors.Wrap(err, "failed to format event")
}

if h.maxFails > 0 && len(execution.Failed()) >= h.maxFails {
return fmt.Errorf("ending test run because max failures was reached")
}
return nil
}

Expand All @@ -59,6 +64,7 @@ func newEventHandler(opts *options) (*eventHandler, error) {
handler := &eventHandler{
formatter: formatter,
err: opts.stderr,
maxFails: opts.maxFails,
}
var err error
if opts.jsonFile != "" {
Expand Down
14 changes: 14 additions & 0 deletions cmd/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"bytes"
"io/ioutil"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -71,3 +72,16 @@ func TestEventHandler_Event_WithMissingActionFail(t *testing.T) {
// of the formatter.
golden.Assert(t, errBuf.String(), "event-handler-missing-test-fail-expected")
}

func TestEventHandler_Event_MaxFails(t *testing.T) {
format := testjson.NewEventFormatter(ioutil.Discard, "testname")

source := golden.Get(t, "../../testjson/testdata/go-test-json.out")
cfg := testjson.ScanConfig{
Stdout: bytes.NewReader(source),
Handler: &eventHandler{formatter: format, maxFails: 2},
}

_, err := testjson.ScanTestOutput(cfg)
assert.Error(t, err, "ending test run because max failures was reached")
}
3 changes: 3 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ func setupFlags(name string) (*pflag.FlagSet, *options) {
"command to run after the tests have completed")
flags.BoolVar(&opts.watch, "watch", false,
"watch go files, and run tests when a file is modified")
flags.IntVar(&opts.maxFails, "max-fails", 0,
"end the test run after this number of failures")

flags.StringVar(&opts.junitFile, "junitfile",
lookEnvWithDefault("GOTESTSUM_JUNITFILE", ""),
Expand Down Expand Up @@ -163,6 +165,7 @@ type options struct {
rerunFailsOnlyRootCases bool
packages []string
watch bool
maxFails int
version bool

// shims for testing
Expand Down
1 change: 1 addition & 0 deletions cmd/testdata/gotestsum-help-text
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Flags:
--junitfile string write a JUnit XML file
--junitfile-testcase-classname field-format format the testcase classname field as: full, relative, short (default full)
--junitfile-testsuite-name field-format format the testsuite name field as: full, relative, short (default full)
--max-fails int end the test run after this number of failures
--no-color disable color output (default true)
--packages list space separated list of package to test
--post-run-command command command to run after the tests have completed
Expand Down

0 comments on commit 3d0c6fa

Please sign in to comment.