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

Make generated Junit file compatable with "Maven Surefire" #488

Merged
merged 3 commits into from
Jun 28, 2018
Merged
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
5 changes: 4 additions & 1 deletion reporters/junit_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package reporters
import (
"encoding/xml"
"fmt"
"math"
"os"
"strings"

Expand All @@ -24,6 +25,7 @@ type JUnitTestSuite struct {
Name string `xml:"name,attr"`
Tests int `xml:"tests,attr"`
Failures int `xml:"failures,attr"`
Errors int `xml:"errors,attr"`
Time float64 `xml:"time,attr"`
}

Expand Down Expand Up @@ -119,8 +121,9 @@ func (reporter *JUnitReporter) SpecDidComplete(specSummary *types.SpecSummary) {

func (reporter *JUnitReporter) SpecSuiteDidEnd(summary *types.SuiteSummary) {
reporter.suite.Tests = summary.NumberOfSpecsThatWillBeRun
reporter.suite.Time = summary.RunTime.Seconds()
reporter.suite.Time = math.Trunc(summary.RunTime.Seconds() * 1000 / 1000)
Copy link
Contributor

Choose a reason for hiding this comment

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

The commit message says "cut testsuite timestamp to three digits after the dot", but does this code really do that? The multiplication + division don't change the value, and then math.Trunc just throws away all sub-second digits.

Perhaps you meant this:
math.Trunc(summary.RunTime.Seconds() * 1000) / 1000

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thank you so much for spotting that @pohly, you are absolutely right. I have opened a PR here #521. What do you think?

reporter.suite.Failures = summary.NumberOfFailedSpecs
reporter.suite.Errors = 0
file, err := os.Create(reporter.filename)
if err != nil {
fmt.Printf("Failed to create JUnit report file: %s\n\t%s", reporter.filename, err.Error())
Expand Down
26 changes: 16 additions & 10 deletions reporters/junit_reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var _ = Describe("JUnit Reporter", func() {
outputFile string
reporter Reporter
)
testSuiteTime := 12345 * time.Millisecond

readOutputFile := func() reporters.JUnitTestSuite {
bytes, err := ioutil.ReadFile(outputFile)
Expand Down Expand Up @@ -70,7 +71,7 @@ var _ = Describe("JUnit Reporter", func() {
reporter.SpecSuiteDidEnd(&types.SuiteSummary{
NumberOfSpecsThatWillBeRun: 1,
NumberOfFailedSpecs: 0,
RunTime: 10 * time.Second,
RunTime: testSuiteTime,
})
})

Expand All @@ -79,7 +80,8 @@ var _ = Describe("JUnit Reporter", func() {
Ω(output.Name).Should(Equal("My test suite"))
Ω(output.Tests).Should(Equal(1))
Ω(output.Failures).Should(Equal(0))
Ω(output.Time).Should(Equal(10.0))
Ω(output.Time).Should(Equal(12.0))
Ω(output.Errors).Should(Equal(0))
Ω(output.TestCases).Should(HaveLen(1))
Ω(output.TestCases[0].Name).Should(Equal("A B C"))
Ω(output.TestCases[0].ClassName).Should(Equal("My test suite"))
Expand Down Expand Up @@ -107,7 +109,7 @@ var _ = Describe("JUnit Reporter", func() {
reporter.SpecSuiteDidEnd(&types.SuiteSummary{
NumberOfSpecsThatWillBeRun: 1,
NumberOfFailedSpecs: 1,
RunTime: 10 * time.Second,
RunTime: testSuiteTime,
})
})

Expand All @@ -116,7 +118,8 @@ var _ = Describe("JUnit Reporter", func() {
Ω(output.Name).Should(Equal("My test suite"))
Ω(output.Tests).Should(Equal(1))
Ω(output.Failures).Should(Equal(1))
Ω(output.Time).Should(Equal(10.0))
Ω(output.Time).Should(Equal(12.0))
Ω(output.Errors).Should(Equal(0))
Ω(output.TestCases[0].Name).Should(Equal("BeforeSuite"))
Ω(output.TestCases[0].Time).Should(Equal(3.0))
Ω(output.TestCases[0].ClassName).Should(Equal("My test suite"))
Expand Down Expand Up @@ -146,7 +149,7 @@ var _ = Describe("JUnit Reporter", func() {
reporter.SpecSuiteDidEnd(&types.SuiteSummary{
NumberOfSpecsThatWillBeRun: 1,
NumberOfFailedSpecs: 1,
RunTime: 10 * time.Second,
RunTime: testSuiteTime,
})
})

Expand All @@ -155,7 +158,8 @@ var _ = Describe("JUnit Reporter", func() {
Ω(output.Name).Should(Equal("My test suite"))
Ω(output.Tests).Should(Equal(1))
Ω(output.Failures).Should(Equal(1))
Ω(output.Time).Should(Equal(10.0))
Ω(output.Time).Should(Equal(12.0))
Ω(output.Errors).Should(Equal(0))
Ω(output.TestCases[0].Name).Should(Equal("AfterSuite"))
Ω(output.TestCases[0].Time).Should(Equal(3.0))
Ω(output.TestCases[0].ClassName).Should(Equal("My test suite"))
Expand Down Expand Up @@ -197,7 +201,7 @@ var _ = Describe("JUnit Reporter", func() {
reporter.SpecSuiteDidEnd(&types.SuiteSummary{
NumberOfSpecsThatWillBeRun: 1,
NumberOfFailedSpecs: 1,
RunTime: 10 * time.Second,
RunTime: testSuiteTime,
})
})

Expand All @@ -206,7 +210,8 @@ var _ = Describe("JUnit Reporter", func() {
Ω(output.Name).Should(Equal("My test suite"))
Ω(output.Tests).Should(Equal(1))
Ω(output.Failures).Should(Equal(1))
Ω(output.Time).Should(Equal(10.0))
Ω(output.Time).Should(Equal(12.0))
Ω(output.Errors).Should(Equal(0))
Ω(output.TestCases[0].Name).Should(Equal("A B C"))
Ω(output.TestCases[0].ClassName).Should(Equal("My test suite"))
Ω(output.TestCases[0].FailureMessage.Type).Should(Equal(specStateCase.message))
Expand Down Expand Up @@ -234,15 +239,16 @@ var _ = Describe("JUnit Reporter", func() {
reporter.SpecSuiteDidEnd(&types.SuiteSummary{
NumberOfSpecsThatWillBeRun: 1,
NumberOfFailedSpecs: 0,
RunTime: 10 * time.Second,
RunTime: testSuiteTime,
})
})

It("should record test as failing", func() {
output := readOutputFile()
Ω(output.Tests).Should(Equal(1))
Ω(output.Failures).Should(Equal(0))
Ω(output.Time).Should(Equal(10.0))
Ω(output.Time).Should(Equal(12.0))
Ω(output.Errors).Should(Equal(0))
Ω(output.TestCases[0].Name).Should(Equal("A B C"))
Ω(output.TestCases[0].Skipped).ShouldNot(BeNil())
})
Expand Down