Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added flag to always include output in junit reports #327

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ accept the following values:
* `relative` - a package path relative to the root of the repository
* `full` - the full package path (default)

When using `--junitfile-always-include-output` the test output is included in the junit report even if the test passed. By default, it's only included if the test failed.


Note: If Go is not installed, or the `go` binary is not in `PATH`, the `GOVERSION`
environment variable can be set to remove the "failed to lookup go version for junit xml"
Expand Down
1 change: 1 addition & 0 deletions cmd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func writeJUnitFile(opts *options, execution *testjson.Execution) error {
FormatTestSuiteName: opts.junitTestSuiteNameFormat.Value(),
FormatTestCaseClassname: opts.junitTestCaseClassnameFormat.Value(),
HideEmptyPackages: opts.junitHideEmptyPackages,
AlwaysIncludeOutput: opts.junitAlwaysIncludeOutput,
})
}

Expand Down
5 changes: 5 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/dnephin/pflag"
"github.com/fatih/color"

"gotest.tools/gotestsum/internal/log"
"gotest.tools/gotestsum/testjson"
)
Expand Down Expand Up @@ -103,6 +104,9 @@ func setupFlags(name string) (*pflag.FlagSet, *options) {
flags.BoolVar(&opts.junitHideEmptyPackages, "junitfile-hide-empty-pkg",
truthyFlag(lookEnvWithDefault("GOTESTSUM_JUNIT_HIDE_EMPTY_PKG", "")),
"omit packages with no tests from the junit.xml file")
flags.BoolVar(&opts.junitAlwaysIncludeOutput, "junitfile-always-include-output",
truthyFlag(lookEnvWithDefault("GOTESTSUM_JUNIT_ALWAYS_INCLUDE_OUTPUT", "")),
"include output even on successful tests")

flags.IntVar(&opts.rerunFailsMaxAttempts, "rerun-fails", 0,
"rerun failed tests until they all pass, or attempts exceeds maximum. Defaults to max 2 reruns when enabled")
Expand Down Expand Up @@ -172,6 +176,7 @@ type options struct {
junitTestCaseClassnameFormat *junitFieldFormatValue
junitProjectName string
junitHideEmptyPackages bool
junitAlwaysIncludeOutput bool
rerunFailsMaxAttempts int
rerunFailsMaxInitialFailures int
rerunFailsReportFile string
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 @@ -13,6 +13,7 @@ Flags:
--jsonfile string write all TestEvents to file
--jsonfile-timing-events string write only the pass, skip, and fail TestEvents to the file
--junitfile string write a JUnit XML file
--junitfile-always-include-output include output even on successful tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe --junit-include-output=all (with a default value of failures) or something like that? "always" I think doesn't make a clear distinction between the two modes: "all output", and "only failure output".

--junitfile-hide-empty-pkg omit packages with no tests from the junit.xml file
--junitfile-project-name string name of the project used in the junit.xml file
--junitfile-testcase-classname field-format format the testcase classname field as: full, relative, short (default full)
Expand Down
42 changes: 33 additions & 9 deletions internal/junitxml/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type JUnitTestCase struct {
Time string `xml:"time,attr"`
SkipMessage *JUnitSkipMessage `xml:"skipped,omitempty"`
Failure *JUnitFailure `xml:"failure,omitempty"`
SystemOut *JUnitSystemOut `xml:"system-out,omitempty"`
}

// JUnitSkipMessage contains the reason why a testcase was skipped.
Expand All @@ -68,12 +69,19 @@ type JUnitFailure struct {
Contents string `xml:",chardata"`
}

type JUnitSystemOut struct {
Message string `xml:"message,attr"`
Type string `xml:"type,attr"`
Contents string `xml:",chardata"`
}

// Config used to write a junit XML document.
type Config struct {
ProjectName string
FormatTestSuiteName FormatFunc
FormatTestCaseClassname FormatFunc
HideEmptyPackages bool
AlwaysIncludeOutput bool
// This is used for tests to have a consistent timestamp
customTimestamp string
customElapsed string
Expand Down Expand Up @@ -114,7 +122,7 @@ func generate(exec *testjson.Execution, cfg Config) JUnitTestSuites {
Tests: pkg.Total,
Time: formatDurationAsSeconds(pkg.Elapsed()),
Properties: packageProperties(version),
TestCases: packageTestCases(pkg, cfg.FormatTestCaseClassname),
TestCases: packageTestCases(pkg, cfg),
Failures: len(pkg.Failed),
Timestamp: cfg.customTimestamp,
}
Expand Down Expand Up @@ -169,13 +177,13 @@ func goVersion() string {
return strings.TrimPrefix(strings.TrimSpace(string(out)), "go version ")
}

func packageTestCases(pkg *testjson.Package, formatClassname FormatFunc) []JUnitTestCase {
func packageTestCases(pkg *testjson.Package, cfg Config) []JUnitTestCase {
cases := []JUnitTestCase{}

var buf bytes.Buffer
pkg.WriteOutputTo(&buf, 0) //nolint:errcheck
if pkg.TestMainFailed() {
var buf bytes.Buffer
pkg.WriteOutputTo(&buf, 0) //nolint:errcheck
jtc := newJUnitTestCase(testjson.TestCase{Test: "TestMain"}, formatClassname)
jtc := newJUnitTestCase(testjson.TestCase{Test: "TestMain"}, cfg.FormatTestCaseClassname)
jtc.Failure = &JUnitFailure{
Message: "Failed",
Contents: buf.String(),
Expand All @@ -184,26 +192,42 @@ func packageTestCases(pkg *testjson.Package, formatClassname FormatFunc) []JUnit
}

for _, tc := range pkg.Failed {
jtc := newJUnitTestCase(tc, formatClassname)
jtc := newJUnitTestCase(tc, cfg.FormatTestCaseClassname)
jtc.Failure = &JUnitFailure{
Message: "Failed",
Contents: strings.Join(pkg.OutputLines(tc), ""),
}
cases = append(cases, jtc)

rerunPassed := false
for _, passedTc := range pkg.Passed {
if passedTc.Test.Name() == tc.Test.Name() {
rerunPassed = true
}
}
if !rerunPassed {
cases = append(cases, jtc)
}
}

for _, tc := range pkg.Skipped {
jtc := newJUnitTestCase(tc, formatClassname)
jtc := newJUnitTestCase(tc, cfg.FormatTestCaseClassname)
jtc.SkipMessage = &JUnitSkipMessage{
Message: strings.Join(pkg.OutputLines(tc), ""),
}
cases = append(cases, jtc)
}

for _, tc := range pkg.Passed {
jtc := newJUnitTestCase(tc, formatClassname)
jtc := newJUnitTestCase(tc, cfg.FormatTestCaseClassname)
if cfg.AlwaysIncludeOutput {
jtc.SystemOut = &JUnitSystemOut{
Message: "Output",
Contents: strings.Join(pkg.OutputLines(tc), ""),
}
}
cases = append(cases, jtc)
}

return cases
}

Expand Down
57 changes: 45 additions & 12 deletions internal/junitxml/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import (
"testing"
"time"

"gotest.tools/gotestsum/testjson"
"gotest.tools/v3/assert"
"gotest.tools/v3/env"
"gotest.tools/v3/golden"

"gotest.tools/gotestsum/testjson"
)

func TestWrite(t *testing.T) {
out := new(bytes.Buffer)
exec := createExecution(t)
exec := createExecution(t, "go-test-json")

env.Patch(t, "GOVERSION", "go7.7.7")
t.Setenv("GOVERSION", "go7.7.7")
err := Write(out, exec, Config{
ProjectName: "test",
customTimestamp: new(time.Time).Format(time.RFC3339),
Expand All @@ -29,11 +29,28 @@ func TestWrite(t *testing.T) {
golden.Assert(t, out.String(), "junitxml-report.golden")
}

func TestWriteWithAlwaysIncludeOutput(t *testing.T) {
out := new(bytes.Buffer)
exec := createExecution(t, "go-test-json")

t.Setenv("GOVERSION", "go7.7.7")
err := Write(out, exec, Config{
ProjectName: "test",
customTimestamp: new(time.Time).Format(time.RFC3339),
customElapsed: "2.1",
AlwaysIncludeOutput: true,
})
assert.NilError(t, err)
golden.Assert(t, out.String(), "junitxml-report-always-include-output.golden")
}



func TestWrite_HideEmptyPackages(t *testing.T) {
out := new(bytes.Buffer)
exec := createExecution(t)
exec := createExecution(t, "go-test-json")

env.Patch(t, "GOVERSION", "go7.7.7")
t.Setenv("GOVERSION", "go7.7.7")
err := Write(out, exec, Config{
ProjectName: "test",
HideEmptyPackages: true,
Expand All @@ -44,24 +61,40 @@ func TestWrite_HideEmptyPackages(t *testing.T) {
golden.Assert(t, out.String(), "junitxml-report-skip-empty.golden")
}

func createExecution(t *testing.T) *testjson.Execution {
func TestWriteReruns(t *testing.T) {
out := new(bytes.Buffer)
exec := createExecution(t, "go-test-json-reruns")

t.Setenv("GOVERSION", "go7.7.7")
err := Write(out, exec, Config{
ProjectName: "test",
HideEmptyPackages: true,
customTimestamp: new(time.Time).Format(time.RFC3339),
customElapsed: "2.1",
})
assert.NilError(t, err)
golden.Assert(t, out.String(), "junitxml-report-reruns.golden")
}

func createExecution(t *testing.T, input string) *testjson.Execution {
exec, err := testjson.ScanTestOutput(testjson.ScanConfig{
Stdout: readTestData(t, "out"),
Stderr: readTestData(t, "err"),
Stdout: readTestData(t, input, "out"),
Stderr: readTestData(t, input, "err"),
})
assert.NilError(t, err)
return exec
}

func readTestData(t *testing.T, stream string) io.Reader {
raw, err := ioutil.ReadFile("../../testjson/testdata/input/go-test-json." + stream)
func readTestData(t *testing.T, input, stream string) io.Reader {
raw, err := ioutil.ReadFile("../../testjson/testdata/input/"+input+"." + stream)
assert.NilError(t, err)
fmt.Println(string(raw))
return bytes.NewReader(raw)
}

func TestGoVersion(t *testing.T) {
t.Run("unknown", func(t *testing.T) {
env.Patch(t, "PATH", "/bogus")
t.Setenv("PATH", "/bogus")
assert.Equal(t, goVersion(), "unknown")
})

Expand Down
Loading