diff --git a/go-junit-report.go b/go-junit-report.go index d001ddf..b003e2e 100644 --- a/go-junit-report.go +++ b/go-junit-report.go @@ -9,11 +9,13 @@ import ( var ( noXMLHeader bool packageName string + setExitCode bool ) func init() { flag.BoolVar(&noXMLHeader, "no-xml-header", false, "do not print xml header") flag.StringVar(&packageName, "package-name", "", "specify a package name (compiled test have no package name in output)") + flag.BoolVar(&setExitCode, "set-exit-code", false, "set exit code to 1 if tests failed") } func main() { @@ -32,4 +34,8 @@ func main() { fmt.Printf("Error writing XML: %s\n", err) os.Exit(1) } + + if setExitCode && report.Failures() > 0 { + os.Exit(1) + } } diff --git a/parser.go b/parser.go index 9092f60..978abe9 100644 --- a/parser.go +++ b/parser.go @@ -148,3 +148,18 @@ func findTest(tests []*Test, name string) *Test { } return nil } + +// Failures counts the number of failed tests in this report +func (r *Report) Failures() int { + count := 0 + + for _, p := range r.Packages { + for _, t := range p.Tests { + if t.Result == FAIL { + count++ + } + } + } + + return count +}