-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Update xUnit to display output on failures #8147
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
Merged
bkhouri
merged 4 commits into
swiftlang:main
from
bkhouri:t/main/improve_xunit_failure_message
Feb 7, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
719c979
Update xUnit to display output on failures (XCTest only)
bkhouri 152c651
Improved the XML escaping and address some comments
bkhouri 317256f
Update Sources/Commands/SwiftTestCommand.swift
bkhouri ca62402
Revert code that is not necessary to have been modified
bkhouri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
Fixtures/Miscellaneous/TestMultipleFailureSwiftTesting/Package.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// swift-tools-version: 6.0 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "TestMultipleFailureSwiftTesting", | ||
targets: [ | ||
.testTarget( | ||
name: "TestMultipleFailureSwiftTestingTests" | ||
) | ||
] | ||
) |
41 changes: 41 additions & 0 deletions
41
...ing/Tests/TestMultipleFailureSwiftTestingTests/TestMultipleFailureSwiftTestingTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import Testing | ||
|
||
@Test func testFailure1() async throws { | ||
#expect(Bool(false), "ST Test failure 1") | ||
} | ||
|
||
@Test func testFailure2() async throws { | ||
#expect(Bool(false), "ST Test failure 2") | ||
} | ||
|
||
@Test func testFailure3() async throws { | ||
#expect(Bool(false), "ST Test failure 3") | ||
} | ||
|
||
@Test func testFailure4() async throws { | ||
#expect(Bool(false), "ST Test failure 4") | ||
} | ||
|
||
@Test func testFailure5() async throws { | ||
#expect(Bool(false), "ST Test failure 5") | ||
} | ||
|
||
@Test func testFailure6() async throws { | ||
#expect(Bool(false), "ST Test failure 6") | ||
} | ||
|
||
@Test func testFailure7() async throws { | ||
#expect(Bool(false), "ST Test failure 7") | ||
} | ||
|
||
@Test func testFailure8() async throws { | ||
#expect(Bool(false), "ST Test failure 8") | ||
} | ||
|
||
@Test func testFailure9() async throws { | ||
#expect(Bool(false), "ST Test failure 9") | ||
} | ||
|
||
@Test func testFailure10() async throws { | ||
#expect(Bool(false), "ST Test failure 10") | ||
} |
13 changes: 13 additions & 0 deletions
13
Fixtures/Miscellaneous/TestMultipleFailureXCTest/Package.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// swift-tools-version: 5.9 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "TestMultipleFailureXCTest", | ||
targets: [ | ||
.testTarget( | ||
name: "TestMultipleFailureXCTestTests" | ||
) | ||
] | ||
) |
44 changes: 44 additions & 0 deletions
44
...leFailureXCTest/Tests/TestMultipleFailureXCTestTests/TestMultipleFailureXCTestTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import XCTest | ||
|
||
final class TestMultipleFailureXCTestTests: XCTestCase { | ||
func testFailure1() throws { | ||
XCTAssertFalse(true, "Test failure 1") | ||
} | ||
|
||
func testFailure2() throws { | ||
XCTAssertFalse(true, "Test failure 2") | ||
} | ||
|
||
func testFailure3() throws { | ||
XCTAssertFalse(true, "Test failure 3") | ||
} | ||
|
||
func testFailure4() throws { | ||
XCTAssertFalse(true, "Test failure 4") | ||
} | ||
|
||
func testFailure5() throws { | ||
XCTAssertFalse(true, "Test failure 5") | ||
} | ||
|
||
func testFailure6() throws { | ||
XCTAssertFalse(true, "Test failure 6") | ||
} | ||
|
||
func testFailure7() throws { | ||
XCTAssertFalse(true, "Test failure 7") | ||
} | ||
|
||
func testFailure8() throws { | ||
XCTAssertFalse(true, "Test failure 8") | ||
} | ||
|
||
func testFailure9() throws { | ||
XCTAssertFalse(true, "Test failure 9") | ||
} | ||
|
||
func testFailure10() throws { | ||
XCTAssertFalse(true, "Test failure 10") | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
Fixtures/Miscellaneous/TestSingleFailureSwiftTesting/Package.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// swift-tools-version: 5.9 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "TestFailuresSwiftTesting", | ||
targets: [ | ||
.testTarget( | ||
name: "TestFailuresSwiftTestingTests" | ||
) | ||
] | ||
) |
5 changes: 5 additions & 0 deletions
5
...ilureSwiftTesting/Tests/TestFailuresSwiftTestingTests/TestFailuresSwiftTestingTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Testing | ||
|
||
@Test func example() async throws { | ||
#expect(Bool(false), "Purposely failing & validating XML espace \"'<>") | ||
} |
13 changes: 13 additions & 0 deletions
13
Fixtures/Miscellaneous/TestSingleFailureXCTest/Package.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// swift-tools-version: 5.9 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "TestFailures", | ||
targets: [ | ||
.testTarget( | ||
name: "TestFailuresTests" | ||
) | ||
] | ||
) |
8 changes: 8 additions & 0 deletions
8
...res/Miscellaneous/TestSingleFailureXCTest/Tests/TestFailuresTests/TestFailuresTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import XCTest | ||
|
||
final class TestFailuresTests: XCTestCase { | ||
func testExample() throws { | ||
XCTAssertFalse(true, "Purposely failing & validating XML espace \"'<>") | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -188,6 +188,15 @@ struct TestCommandOptions: ParsableArguments { | |
help: "Path where the xUnit xml file should be generated.") | ||
var xUnitOutput: AbsolutePath? | ||
|
||
@Flag( | ||
name: .customLong("experimental-xunit-message-failure"), | ||
help: ArgumentHelp( | ||
"When set, include the content of stdout/stderr in failure messages (XCTest only, experimental).", | ||
visibility: .hidden | ||
) | ||
) | ||
var shouldShowDetailedFailureMessage: Bool = false | ||
|
||
/// Generate LinuxMain entries and exit. | ||
@Flag(name: .customLong("testable-imports"), inversion: .prefixedEnableDisable, help: "Enable or disable testable imports. Enabled by default.") | ||
var enableTestableImports: Bool = true | ||
|
@@ -416,7 +425,10 @@ public struct SwiftTestCommand: AsyncSwiftCommand { | |
fileSystem: swiftCommandState.fileSystem, | ||
results: testResults | ||
) | ||
try generator.generate(at: xUnitOutput) | ||
try generator.generate( | ||
at: xUnitOutput, | ||
detailedFailureMessage: self.options.shouldShowDetailedFailureMessage | ||
) | ||
} | ||
|
||
// MARK: - Common implementation | ||
|
@@ -1371,7 +1383,7 @@ final class XUnitGenerator { | |
} | ||
|
||
/// Generate the file at the given path. | ||
func generate(at path: AbsolutePath) throws { | ||
func generate(at path: AbsolutePath, detailedFailureMessage: Bool) throws { | ||
var content = | ||
""" | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
@@ -1404,7 +1416,8 @@ final class XUnitGenerator { | |
""" | ||
|
||
if !result.success { | ||
content += "<failure message=\"failed\"></failure>\n" | ||
let failureMessage = detailedFailureMessage ? result.output.map(_escapeForXML).joined() : "failure" | ||
content += "<failure message=\"\(failureMessage)\"></failure>\n" | ||
} | ||
|
||
content += "</testcase>\n" | ||
|
@@ -1421,6 +1434,32 @@ final class XUnitGenerator { | |
} | ||
} | ||
|
||
/// Escape a single Unicode character for use in an XML-encoded string. | ||
/// | ||
/// - Parameters: | ||
/// - character: The character to escape. | ||
/// | ||
/// - Returns: `character`, or a string containing its escaped form. | ||
private func _escapeForXML(_ character: Character) -> String { | ||
switch character { | ||
case "\"": | ||
""" | ||
case "<": | ||
"<" | ||
case ">": | ||
">" | ||
case "&": | ||
"&" | ||
case _ where !character.isASCII || character.isNewline: | ||
character.unicodeScalars.lazy | ||
.map(\.value) | ||
.map { "&#\($0);" } | ||
.joined() | ||
default: | ||
String(character) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: tab went missing |
||
} | ||
} | ||
|
||
extension SwiftCommandState { | ||
func buildParametersForTest( | ||
options: TestCommandOptions | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.