Skip to content

Commit

Permalink
Option to print output into report, when tests have passed
Browse files Browse the repository at this point in the history
This is PR for #583

A new CLI option: -ginkgo.reportPassed  :
It will print output for each passed test in the generated report,
including JUnit, Teamcity, and Default reporters.

For example, in JUnit (XML), the test output will be added under:
`<testcase> <passed>`

The default behavior (without this option), prints test output
only if the test case (spec) has failed.
  • Loading branch information
manosnoam committed Jul 1, 2019
1 parent db0f5f9 commit f735d68
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 18 deletions.
2 changes: 2 additions & 0 deletions internal/specrunner/spec_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,7 @@ var _ = Describe("Spec Runner", func() {
"R1.WillRun",
"R2.WillRun",
"A",
"BYTES",
"R2.DidComplete",
"R1.DidComplete",
"TRUNCATE",
Expand All @@ -734,6 +735,7 @@ var _ = Describe("Spec Runner", func() {
"R1.WillRun",
"R2.WillRun",
"C",
"BYTES",
"R2.DidComplete",
"R1.DidComplete",
}))
Expand Down
6 changes: 4 additions & 2 deletions reporters/default_reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,11 @@ var _ = Describe("DefaultReporter", func() {
BeforeEach(func() {
reporterConfig.ReportPassed = true
reporter = reporters.NewDefaultReporter(reporterConfig, stenographer)
spec.CapturedOutput = "test scenario"
})

It("should announce the captured output", func() {
Ω(stenographer.Calls()[0]).Should(Equal(call("AnnounceCapturedOutput", spec)))
Ω(stenographer.Calls()[1]).Should(Equal(call("AnnounceCapturedOutput", spec.CapturedOutput)))
})
})
})
Expand Down Expand Up @@ -381,10 +382,11 @@ var _ = Describe("DefaultReporter", func() {
BeforeEach(func() {
reporterConfig.ReportPassed = true
reporter = reporters.NewDefaultReporter(reporterConfig, stenographer)
spec.CapturedOutput = "test scenario"
})

It("should announce the captured output", func() {
Ω(stenographer.Calls()[0]).Should(Equal(call("AnnounceCapturedOutput", spec)))
Ω(stenographer.Calls()[1]).Should(Equal(call("AnnounceCapturedOutput", spec.CapturedOutput)))
})
})
})
Expand Down
6 changes: 3 additions & 3 deletions reporters/junit_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type JUnitReporter struct {
suite JUnitTestSuite
filename string
testSuiteName string
reporterConfig config.DefaultReporterConfigType
ReporterConfig config.DefaultReporterConfigType
}

//NewJUnitReporter creates a new JUnit XML reporter. The XML will be stored in the passed in filename.
Expand All @@ -72,7 +72,7 @@ func (reporter *JUnitReporter) SpecSuiteWillBegin(ginkgoConfig config.GinkgoConf
TestCases: []JUnitTestCase{},
}
reporter.testSuiteName = summary.SuiteDescription
reporter.reporterConfig = config.DefaultReporterConfig
reporter.ReporterConfig = config.DefaultReporterConfig
}

func (reporter *JUnitReporter) SpecWillRun(specSummary *types.SpecSummary) {
Expand Down Expand Up @@ -112,7 +112,7 @@ func (reporter *JUnitReporter) SpecDidComplete(specSummary *types.SpecSummary) {
Name: strings.Join(specSummary.ComponentTexts[1:], " "),
ClassName: reporter.testSuiteName,
}
if reporter.reporterConfig.ReportPassed && specSummary.State == types.SpecStatePassed {
if reporter.ReporterConfig.ReportPassed && specSummary.State == types.SpecStatePassed {
testCase.PassedMessage = &JUnitPassedMessage{
Message: specSummary.CapturedOutput,
}
Expand Down
14 changes: 6 additions & 8 deletions reporters/junit_reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import (
var _ = Describe("JUnit Reporter", func() {
var (
outputFile string
reporter reporters.JUnitReporter
reporterConfig config.DefaultReporterConfigType
reporter *reporters.JUnitReporter
)
testSuiteTime := 12456999 * time.Microsecond
reportedSuiteTime := 12.456
Expand Down Expand Up @@ -55,20 +54,19 @@ var _ = Describe("JUnit Reporter", func() {
beforeSuite := &types.SetupSummary{
State: types.SpecStatePassed,
}

//Set the ReportPassed config flag, in order show captured output in JUnit XML, when tests have passed.
reporterConfig.ReportPassed = true

reporter.BeforeSuiteDidRun(beforeSuite)

afterSuite := &types.SetupSummary{
State: types.SpecStatePassed,
}
reporter.AfterSuiteDidRun(afterSuite)

// Set the ReportPassed config flag, in order to show captured output when tests have passed.
reporter.ReporterConfig.ReportPassed = true

spec := &types.SpecSummary{
ComponentTexts: []string{"[Top Level]", "A", "B", "C"},
CapturedOutput: "Spec output: My test case",
CapturedOutput: "Test scenario...",
State: types.SpecStatePassed,
RunTime: 5 * time.Second,
}
Expand All @@ -95,7 +93,7 @@ var _ = Describe("JUnit Reporter", func() {
Ω(output.TestCases[0].FailureMessage).Should(BeNil())
Ω(output.TestCases[0].Skipped).Should(BeNil())
Ω(output.TestCases[0].Time).Should(Equal(5.0))
Ω(output.TestCases[0].PassedMessage.Message).Should(ContainSubstring("My test case"))
Ω(output.TestCases[0].PassedMessage.Message).Should(ContainSubstring("Test scenario"))
})
})

Expand Down
8 changes: 4 additions & 4 deletions reporters/teamcity_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ const (
)

type TeamCityReporter struct {
writer io.Writer
testSuiteName string
config config.DefaultReporterConfigType
writer io.Writer
testSuiteName string
ReporterConfig config.DefaultReporterConfigType
}

func NewTeamCityReporter(writer io.Writer) *TeamCityReporter {
Expand Down Expand Up @@ -66,7 +66,7 @@ func (reporter *TeamCityReporter) SpecWillRun(specSummary *types.SpecSummary) {
func (reporter *TeamCityReporter) SpecDidComplete(specSummary *types.SpecSummary) {
testName := escape(strings.Join(specSummary.ComponentTexts[1:], " "))

if reporter.config.ReportPassed && specSummary.State == types.SpecStatePassed {
if reporter.ReporterConfig.ReportPassed && specSummary.State == types.SpecStatePassed {
details := escape(specSummary.CapturedOutput)
fmt.Fprintf(reporter.writer, "%s[testPassed name='%s' details='%s']", messageId, testName, details)
}
Expand Down
7 changes: 6 additions & 1 deletion reporters/teamcity_reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
var _ = Describe("TeamCity Reporter", func() {
var (
buffer bytes.Buffer
reporter Reporter
reporter *reporters.TeamCityReporter
)

BeforeEach(func() {
Expand All @@ -40,8 +40,12 @@ var _ = Describe("TeamCity Reporter", func() {
}
reporter.AfterSuiteDidRun(afterSuite)

// Set the ReportPassed config flag, in order to show captured output when tests have passed.
reporter.ReporterConfig.ReportPassed = true

spec := &types.SpecSummary{
ComponentTexts: []string{"[Top Level]", "A", "B", "C"},
CapturedOutput: "Test scenario...",
State: types.SpecStatePassed,
RunTime: 5 * time.Second,
}
Expand All @@ -60,6 +64,7 @@ var _ = Describe("TeamCity Reporter", func() {
expected :=
"##teamcity[testSuiteStarted name='Foo|'s test suite']" +
"##teamcity[testStarted name='A B C']" +
"##teamcity[testPassed name='A B C' details='Test scenario...']" +
"##teamcity[testFinished name='A B C' duration='5000']" +
"##teamcity[testSuiteFinished name='Foo|'s test suite']"
Ω(actual).Should(Equal(expected))
Expand Down

0 comments on commit f735d68

Please sign in to comment.