From a34e6b23658155d525ea5b5472722a40ca0ce65c Mon Sep 17 00:00:00 2001 From: itamarb Date: Sun, 26 Apr 2015 19:54:56 +0300 Subject: [PATCH] adding 'set-exit-code' flag Defaults to false. When true, sets the exit-code to 1 if any tests failed. This is useful for automation. --- go-junit-report.go | 6 ++++++ parser.go | 15 +++++++++++++++ 2 files changed, 21 insertions(+) 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 +}