Skip to content
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
15 changes: 11 additions & 4 deletions Sources/Testing/Events/Recorder/Event.JUnitXMLRecorder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,15 @@ extension Event.JUnitXMLRecorder {
let classNameComponents = CollectionOfOne(id.moduleName) + id.nameComponents.dropLast()
let className = classNameComponents.joined(separator: ".")
let name = id.nameComponents.last!
let durationNanoseconds = testData.startInstant.nanoseconds(until: testData.endInstant ?? .now)
let durationSeconds = Double(durationNanoseconds) / 1_000_000_000

// Tests that are skipped or for some reason never completed will not have
// an end instant; don't report timing for such tests.
var timeClause = ""
if let endInstant = testData.endInstant {
let durationNanoseconds = testData.startInstant.nanoseconds(until: endInstant)
let durationSeconds = Double(durationNanoseconds) / 1_000_000_000
timeClause = #"time="\#(durationSeconds)" "#
}

// Build out any child nodes contained within this <testcase> node.
var minutiae = [String]()
Expand All @@ -193,9 +200,9 @@ extension Event.JUnitXMLRecorder {
}

if minutiae.isEmpty {
result.append(#" <testcase classname="\#(className)" name="\#(name)" time="\#(durationSeconds)" />"#)
result.append(#" <testcase classname="\#(className)" name="\#(name)" \#(timeClause)/>"#)
} else {
result.append(#" <testcase classname="\#(className)" name="\#(name)" time="\#(durationSeconds)">"#)
result.append(#" <testcase classname="\#(className)" name="\#(name)" \#(timeClause)>"#)
result += minutiae
result.append(#" </testcase>"#)
}
Expand Down
22 changes: 22 additions & 0 deletions Tests/TestingTests/EventRecorderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,28 @@ struct EventRecorderTests {
throw caughtError
}
}

@Test(
"JUnit XML omits time for skipped tests",
.bug("https://github.com/swiftlang/swift-testing/issues/740")
)
func junitXMLWithTimelessSkippedTest() async throws {
let stream = Stream()

let eventRecorder = Event.JUnitXMLRecorder(writingUsing: stream.write)
eventRecorder.record(Event(.runStarted, testID: nil, testCaseID: nil), in: Event.Context(test: nil, testCase: nil, configuration: nil))
let test = Test {}
eventRecorder.record(Event(.testSkipped(.init(sourceContext: .init())), testID: test.id, testCaseID: nil), in: Event.Context(test: test, testCase: nil, configuration: nil))
eventRecorder.record(Event(.runEnded, testID: nil, testCaseID: nil), in: Event.Context(test: nil, testCase: nil, configuration: nil))

let xmlString = stream.buffer.rawValue
#expect(xmlString.hasPrefix("<?xml"))
let testCaseLines = xmlString
.split(whereSeparator: \.isNewline)
.filter { $0.contains("<testcase") }
#expect(!testCaseLines.isEmpty)
#expect(!testCaseLines.contains { $0.contains("time=") })
}
#endif

@Test("HumanReadableOutputRecorder counts issues without associated tests")
Expand Down