Skip to content

[SwiftTestTool] Add test failure messages to xUnit report #6174

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cyberowl
Copy link

Motivation

The xUnit test run report does not provide detailed failure information and this makes it very difficult to investigate problems without additional viewing of stderr logs on some CI systems

Modifications

  1. Added the use of regular expressions to parse the output of test failures and crashes too (i think it's a simplest approach at this moment)
  2. Added line breaks to the output of parallel testing in case of regular expressions usage
  3. Added test coverage for introduced changes (except "Fatal Error" case for a known reason)

Result

Better xUnit report with test failures

Before:

<testsuites>
<testsuite name="TestResults" errors="0" tests="5" failures="3" time="21.489489211">
<testcase classname="ParallelTestsPkgTests.ParallelTestsFailureTests" name="testExpectationFailure" time="4.297888375">
<failure message="failed"></failure>
</testcase>
<testcase classname="ParallelTestsPkgTests.ParallelTestsFailureTests" name="testAssertionFailure" time="4.297924834">
<failure message="failed"></failure>
</testcase>
<testcase classname="ParallelTestsPkgTests.ParallelTestsTests" name="testExample1" time="4.297897584">
</testcase>
<testcase classname="ParallelTestsPkgTests.ParallelTestsTests" name="testExample2" time="4.297898459">
</testcase>
<testcase classname="ParallelTestsPkgTests.ParallelTestsFailureTests" name="testSureFailure" time="4.297879959">
<failure message="failed"></failure>
</testcase>
</testsuite>
</testsuites>

After:

<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="TestResults" errors="0" tests="5" failures="3" time="15.821316957">
<testcase classname="ParallelTestsPkgTests.ParallelTestsTests" name="testExample2" time="0.04732175">
</testcase>
<testcase classname="ParallelTestsPkgTests.ParallelTestsFailureTests" name="testExpectationFailure" time="3.29376925">
<failure message="ParallelTestsFailureTests.swift:16 Asynchronous wait failed: Exceeded timeout of 0 seconds, with unfulfilled expectations: &quot;failing expectation&quot;."></failure>
</testcase>
<testcase classname="ParallelTestsPkgTests.ParallelTestsTests" name="testExample1" time="4.159991583">
</testcase>
<testcase classname="ParallelTestsPkgTests.ParallelTestsFailureTests" name="testSureFailure" time="4.160105583">
<failure message="ParallelTestsFailureTests.swift:7 failed - Giving up is the only sure way to fail."></failure>
</testcase>
<testcase classname="ParallelTestsPkgTests.ParallelTestsFailureTests" name="testAssertionFailure" time="4.160128791">
<failure message="ParallelTestsFailureTests.swift:11 XCTAssertTrue failed - Expected assertion failure."></failure>
</testcase>
</testsuite>
</testsuites>

.joined(separator: " ")
.xmlEscaped ?? "failed"

stream <<< "<failure message=\"\(message)\"></failure>\n"
Copy link
Contributor

Choose a reason for hiding this comment

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

this seems like an improvement over the hard coded "failed" message, but a bit fragile. @stmontgomery any advice?

Copy link
Contributor

Choose a reason for hiding this comment

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

It really should not be necessary to scrape the textual logging output of XCTests and parse it using regex. We have a proper API for observing events and retrieving this data in structured ways: XCTestObservation.

However, I think SwiftPM needs to coordinate doing this for it to be effective but it doesn't do that today. So I can understand why this approach relying on more-and-more intricate regex parsing is seen as the easiest solution at the moment. I believe SwiftPM already parses the textual logging output to a certain extent (right?), so this is only adding a bit more and I'm not going to stand in the way of landing this. But I think we really should spend some time building the necessary infrastructure for SwiftPM to coordinate more closely with the runner process to gather its results in a structured way, to avoid the need for any of this.

@tomerd Do you know if we already have an Issue tracking this sort of enhancement on the SwiftPM side?

Copy link
Contributor

@tomerd tomerd Feb 23, 2023

Choose a reason for hiding this comment

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

@stmontgomery we have been pushing back on adding parsing of XCTest output and we would very much like to see a structured output from XCTest instead of the non-structured one we have now - it will help solve several issues. iirc we looked at this before and the conclusion was that the invocation of XCTest goes through several layers that essentially shell out so there is little room for setting up delegates etc, but maybe there was a flag we can pass to the process that can assist? open to ideas on how to be at achieve this

@tomerd tomerd self-assigned this Feb 24, 2023
@grynspan grynspan added the swift test Changes impacting `swift test` tool label Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
swift test Changes impacting `swift test` tool
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants