Swift Testing giving worse error messages than XCTest

UPDATE:

I was not using my exact code in the first posting. The following is the crucial point:


When using

XCTAssertEqual(myFunction(), "the value")

and myFunction() does not return the desired value, then in the according error message I immediately get both values for a quick comparison, which is very nice.

But when using Swift Testing:

#expect(myFunction(), "the value")

I only get the error message "Expectation failed: myFunction() == "the value".

I have to add additional code to know what the wrong value is.

Is this something that could be changed, or is it an inherent disadvantage when using Swift Testing? How do you deal with it?

Not sure if this is just a typo, but use == rather than , if you are comparing them for equality.

If you're using Xcode, more information is available (including the values of the operands above) if you click the red test issue banner to expand it or if you look at the test report. For more information, take a look at Meet Swift Testing from WWDC24.

1 Like

Yes.

Cannot see more when clicking the red banner, maybe that is currently broken.

Not as far as I know. What version of Xcode are you using? And are you using Swift Testing as a package dependency? If so, Xcode's integration with Swift Testing is much more limited when you include it as a package dependency. Xcode 16.0 and later ship with Swift Testing built in, so you can use that built-in copy instead.

Aha, for simple comparisons of Strings with variables there is indeed a very good message:

Expectation failed: (myValue → "...produced...") == "...expected..."

It less good when calling a function (in the following example, I even compare the result with an array, but this in not the problem):

#expect(try myFunction(withInput: "XXXX") == ["YYYYY", "ZZZZ"])

Message as described in my first post.

And when writing (as I did in my original code):

#expect(
    try myFunction(withInput: "XXXX") ==
    ["YYYYY", "ZZZZ"]
)

the layout of the message is a little bit awkward (but yes, clicking on the icon works).

I am using Xcode 16.4 with Swift 6.1.2, just using import Testing, no package dependency.

It's expected that we don't expand the expression if there's a try or await keyword. See Value not shown in error message when an expectation condition has effects (try/await) · Issue #162 · swiftlang/swift-testing · GitHub.

You can work around this constraint by breaking up the expression:

let foo = try myFunction(withInput: "XXXX")
#expect(foo == ["YYYYY", "ZZZZ"])
3 Likes

Thanks, my first posting was not exact enough, sorry. I added an update note at the beginning of the first post.