Skip to content

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
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
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"
)
]
)
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 Fixtures/Miscellaneous/TestMultipleFailureXCTest/Package.swift
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"
)
]
)
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")
}

}
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"
)
]
)
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 Fixtures/Miscellaneous/TestSingleFailureXCTest/Package.swift
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"
)
]
)
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 \"'<>")
}
}

45 changes: 42 additions & 3 deletions Sources/Commands/SwiftTestCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"?>
Expand Down Expand Up @@ -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"
Expand All @@ -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 "\"":
"&quot;"
case "<":
"&lt;"
case ">":
"&gt;"
case "&":
"&amp;"
case _ where !character.isASCII || character.isNewline:
character.unicodeScalars.lazy
.map(\.value)
.map { "&#\($0);" }
.joined()
default:
String(character)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: tab went missing

}
}

extension SwiftCommandState {
func buildParametersForTest(
options: TestCommandOptions
Expand Down
9 changes: 7 additions & 2 deletions Sources/_InternalTestSupport/SwiftPMProduct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,15 @@ extension SwiftPM {
/// - args: The arguments to pass.
/// - env: Additional environment variables to pass. The values here are merged with default env.
/// - packagePath: Adds argument `--package-path <path>` if not nil.
/// - throwIfCommandFails: If set, will throw an error if the command does not have a 0 return code.
///
/// - Returns: The output of the process.
@discardableResult
public func execute(
_ args: [String] = [],
packagePath: AbsolutePath? = nil,
env: Environment? = nil
env: Environment? = nil,
throwIfCommandFails: Bool = true
) async throws -> (stdout: String, stderr: String) {
let result = try await executeProcess(
args,
Expand All @@ -93,8 +95,11 @@ extension SwiftPM {
let stdout = try result.utf8Output()
let stderr = try result.utf8stderrOutput()

let returnValue = (stdout: stdout, stderr: stderr)
if (!throwIfCommandFails) { return returnValue }

if result.exitStatus == .terminated(code: 0) {
return (stdout: stdout, stderr: stderr)
return returnValue
}
throw SwiftPMError.executionFailure(
underlying: AsyncProcessResult.Error.nonZeroExit(result),
Expand Down
Loading