Skip to content

Commit

Permalink
teamcity reporter: output location of test failure as well as test de…
Browse files Browse the repository at this point in the history
…finition (#626)

Previously, only the test definition was output. When a test fails, the
location of the failure is important to know, especially for complex tests.

Signed-off-by: Daniel Lipovetsky <dlipovetsky@d2iq.com>
  • Loading branch information
dlipovetsky authored and blgm committed Nov 28, 2019
1 parent 3cfa02d commit 9869142
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
16 changes: 12 additions & 4 deletions reporters/teamcity_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func (reporter *TeamCityReporter) handleSetupSummary(name string, setupSummary *
if setupSummary.State != types.SpecStatePassed {
testName := escape(name)
fmt.Fprintf(reporter.writer, "%s[testStarted name='%s']\n", messageId, testName)
message := escape(setupSummary.Failure.ComponentCodeLocation.String())
details := escape(setupSummary.Failure.Message)
message := reporter.failureMessage(setupSummary.Failure)
details := reporter.failureDetails(setupSummary.Failure)
fmt.Fprintf(reporter.writer, "%s[testFailed name='%s' message='%s' details='%s']\n", messageId, testName, message, details)
durationInMilliseconds := setupSummary.RunTime.Seconds() * 1000
fmt.Fprintf(reporter.writer, "%s[testFinished name='%s' duration='%v']\n", messageId, testName, durationInMilliseconds)
Expand All @@ -71,8 +71,8 @@ func (reporter *TeamCityReporter) SpecDidComplete(specSummary *types.SpecSummary
fmt.Fprintf(reporter.writer, "%s[testPassed name='%s' details='%s']\n", messageId, testName, details)
}
if specSummary.State == types.SpecStateFailed || specSummary.State == types.SpecStateTimedOut || specSummary.State == types.SpecStatePanicked {
message := escape(specSummary.Failure.ComponentCodeLocation.String())
details := escape(specSummary.Failure.Message)
message := reporter.failureMessage(specSummary.Failure)
details := reporter.failureDetails(specSummary.Failure)
fmt.Fprintf(reporter.writer, "%s[testFailed name='%s' message='%s' details='%s']\n", messageId, testName, message, details)
}
if specSummary.State == types.SpecStateSkipped || specSummary.State == types.SpecStatePending {
Expand All @@ -87,6 +87,14 @@ func (reporter *TeamCityReporter) SpecSuiteDidEnd(summary *types.SuiteSummary) {
fmt.Fprintf(reporter.writer, "%s[testSuiteFinished name='%s']\n", messageId, reporter.testSuiteName)
}

func (reporter *TeamCityReporter) failureMessage(failure types.SpecFailure) string {
return escape(failure.ComponentCodeLocation.String())
}

func (reporter *TeamCityReporter) failureDetails(failure types.SpecFailure) string {
return escape(fmt.Sprintf("%s\n%s", failure.Message, failure.Location.String()))
}

func escape(output string) string {
output = strings.Replace(output, "|", "||", -1)
output = strings.Replace(output, "'", "|'", -1)
Expand Down
22 changes: 16 additions & 6 deletions reporters/teamcity_reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ var _ = Describe("TeamCity Reporter", func() {
Failure: types.SpecFailure{
Message: "failed to setup\n",
ComponentCodeLocation: codelocation.New(0),
Location: codelocation.New(2),
},
}
reporter.BeforeSuiteDidRun(beforeSuite)
Expand All @@ -97,9 +98,11 @@ var _ = Describe("TeamCity Reporter", func() {
expected := fmt.Sprintf(
"##teamcity[testSuiteStarted name='Foo|'s test suite']\n"+
"##teamcity[testStarted name='BeforeSuite']\n"+
"##teamcity[testFailed name='BeforeSuite' message='%s' details='failed to setup|n']\n"+
"##teamcity[testFailed name='BeforeSuite' message='%s' details='failed to setup|n|n%s']\n"+
"##teamcity[testFinished name='BeforeSuite' duration='3000']\n"+
"##teamcity[testSuiteFinished name='Foo|'s test suite']\n", beforeSuite.Failure.ComponentCodeLocation.String(),
"##teamcity[testSuiteFinished name='Foo|'s test suite']\n",
beforeSuite.Failure.ComponentCodeLocation.String(),
beforeSuite.Failure.Location.String(),
)
Ω(actual).Should(Equal(expected))
})
Expand All @@ -115,6 +118,7 @@ var _ = Describe("TeamCity Reporter", func() {
Failure: types.SpecFailure{
Message: "failed to setup\n",
ComponentCodeLocation: codelocation.New(0),
Location: codelocation.New(2),
},
}
reporter.AfterSuiteDidRun(afterSuite)
Expand All @@ -131,9 +135,11 @@ var _ = Describe("TeamCity Reporter", func() {
expected := fmt.Sprintf(
"##teamcity[testSuiteStarted name='Foo|'s test suite']\n"+
"##teamcity[testStarted name='AfterSuite']\n"+
"##teamcity[testFailed name='AfterSuite' message='%s' details='failed to setup|n']\n"+
"##teamcity[testFailed name='AfterSuite' message='%s' details='failed to setup|n|n%s']\n"+
"##teamcity[testFinished name='AfterSuite' duration='3000']\n"+
"##teamcity[testSuiteFinished name='Foo|'s test suite']\n", afterSuite.Failure.ComponentCodeLocation.String(),
"##teamcity[testSuiteFinished name='Foo|'s test suite']\n",
afterSuite.Failure.ComponentCodeLocation.String(),
afterSuite.Failure.Location.String(),
)
Ω(actual).Should(Equal(expected))
})
Expand All @@ -158,6 +164,7 @@ var _ = Describe("TeamCity Reporter", func() {
RunTime: 5 * time.Second,
Failure: types.SpecFailure{
ComponentCodeLocation: codelocation.New(0),
Location: codelocation.New(2),
Message: "I failed",
},
}
Expand All @@ -176,9 +183,12 @@ var _ = Describe("TeamCity Reporter", func() {
expected :=
fmt.Sprintf("##teamcity[testSuiteStarted name='Foo|'s test suite']\n"+
"##teamcity[testStarted name='A B C']\n"+
"##teamcity[testFailed name='A B C' message='%s' details='I failed']\n"+
"##teamcity[testFailed name='A B C' message='%s' details='I failed|n%s']\n"+
"##teamcity[testFinished name='A B C' duration='5000']\n"+
"##teamcity[testSuiteFinished name='Foo|'s test suite']\n", spec.Failure.ComponentCodeLocation.String())
"##teamcity[testSuiteFinished name='Foo|'s test suite']\n",
spec.Failure.ComponentCodeLocation.String(),
spec.Failure.Location.String(),
)
Ω(actual).Should(Equal(expected))
})
})
Expand Down

0 comments on commit 9869142

Please sign in to comment.