Skip to content

Commit

Permalink
Option to print output of passed tests in Junit XML result
Browse files Browse the repository at this point in the history
This is PR for onsi#583

A new CLI option: -ginkgo.junitPassed.
It will add output for each passed test into the JUnit XML, under
<testcase> <passed>. (The default is only for failed tests output).
  • Loading branch information
manosnoam committed Jun 25, 2019
1 parent 4cb7441 commit 26f0456
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type DefaultReporterConfigType struct {
Succinct bool
Verbose bool
FullTrace bool
JunitPassed bool
}

var DefaultReporterConfig = DefaultReporterConfigType{}
Expand Down Expand Up @@ -98,6 +99,7 @@ func Flags(flagSet *flag.FlagSet, prefix string, includeParallelFlags bool) {
flagSet.BoolVar(&(DefaultReporterConfig.Verbose), prefix+"v", false, "If set, default reporter print out all specs as they begin.")
flagSet.BoolVar(&(DefaultReporterConfig.Succinct), prefix+"succinct", false, "If set, default reporter prints out a very succinct report")
flagSet.BoolVar(&(DefaultReporterConfig.FullTrace), prefix+"trace", false, "If set, default reporter prints out the full stack trace when a failure occurs")
flagSet.BoolVar(&(DefaultReporterConfig.JunitPassed), prefix+"junitPassed", false, "If set, JUnit reporter prints out steps of passed tests in the XML result.")
}

func BuildFlagArgs(prefix string, ginkgo GinkgoConfigType, reporter DefaultReporterConfigType) []string {
Expand Down Expand Up @@ -196,5 +198,9 @@ func BuildFlagArgs(prefix string, ginkgo GinkgoConfigType, reporter DefaultRepor
result = append(result, fmt.Sprintf("--%strace", prefix))
}

if reporter.JunitPassed {
result = append(result, fmt.Sprintf("--%sjunitPassed", prefix))
}

return result
}
2 changes: 1 addition & 1 deletion internal/specrunner/spec_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func (runner *SpecRunner) reportSpecWillRun(summary *types.SpecSummary) {
}

func (runner *SpecRunner) reportSpecDidComplete(summary *types.SpecSummary, failed bool) {
if failed && len(summary.CapturedOutput) == 0 {
if len(summary.CapturedOutput) == 0 && (failed || config.DefaultReporterConfig.JunitPassed) {
summary.CapturedOutput = string(runner.writer.Bytes())
}
for i := len(runner.reporters) - 1; i >= 1; i-- {
Expand Down
1 change: 1 addition & 0 deletions reporters/default_reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var _ = Describe("DefaultReporter", func() {
NoisySkippings: false,
Verbose: true,
FullTrace: true,
JunitPassed: true,
}

reporter = reporters.NewDefaultReporter(reporterConfig, stenographer)
Expand Down
20 changes: 16 additions & 4 deletions reporters/junit_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ type JUnitTestSuite struct {
type JUnitTestCase struct {
Name string `xml:"name,attr"`
ClassName string `xml:"classname,attr"`
PassedMessage *JUnitPassedMessage `xml:"passed,omitempty"`
FailureMessage *JUnitFailureMessage `xml:"failure,omitempty"`
Skipped *JUnitSkipped `xml:"skipped,omitempty"`
Time float64 `xml:"time,attr"`
SystemOut string `xml:"system-out,omitempty"`
}

type JUnitPassedMessage struct {
Message string `xml:",chardata"`
}

type JUnitFailureMessage struct {
Type string `xml:"type,attr"`
Message string `xml:",chardata"`
Expand All @@ -48,9 +53,10 @@ type JUnitSkipped struct {
}

type JUnitReporter struct {
suite JUnitTestSuite
filename string
testSuiteName string
suite JUnitTestSuite
filename string
testSuiteName string
reporterConfig config.DefaultReporterConfigType
}

//NewJUnitReporter creates a new JUnit XML reporter. The XML will be stored in the passed in filename.
Expand All @@ -60,12 +66,13 @@ func NewJUnitReporter(filename string) *JUnitReporter {
}
}

func (reporter *JUnitReporter) SpecSuiteWillBegin(config config.GinkgoConfigType, summary *types.SuiteSummary) {
func (reporter *JUnitReporter) SpecSuiteWillBegin(ginkgoConfig config.GinkgoConfigType, summary *types.SuiteSummary) {
reporter.suite = JUnitTestSuite{
Name: summary.SuiteDescription,
TestCases: []JUnitTestCase{},
}
reporter.testSuiteName = summary.SuiteDescription
reporter.reporterConfig = config.DefaultReporterConfig
}

func (reporter *JUnitReporter) SpecWillRun(specSummary *types.SpecSummary) {
Expand Down Expand Up @@ -105,6 +112,11 @@ func (reporter *JUnitReporter) SpecDidComplete(specSummary *types.SpecSummary) {
Name: strings.Join(specSummary.ComponentTexts[1:], " "),
ClassName: reporter.testSuiteName,
}
if reporter.reporterConfig.JunitPassed && specSummary.State == types.SpecStatePassed {
testCase.PassedMessage = &JUnitPassedMessage{
Message: specSummary.CapturedOutput,
}
}
if specSummary.State == types.SpecStateFailed || specSummary.State == types.SpecStateTimedOut || specSummary.State == types.SpecStatePanicked {
testCase.FailureMessage = &JUnitFailureMessage{
Type: reporter.failureTypeForState(specSummary.State),
Expand Down

0 comments on commit 26f0456

Please sign in to comment.