swift-testing icon indicating copy to clipboard operation
swift-testing copied to clipboard

thrown error from a naked try (not inside #expect/#require) from a test does not report correct line number

Open bstecher1 opened this issue 11 months ago • 2 comments

Description

If you have a test case like

@Test func test1() throws { a = try getAnObject() }

and getAnObject() throws an error, the test framework reports the @Test line number as where the problem occurs, not the getAnObject() line

Reproduction

testfail.zip

Unzip the above archive, then cd into it and type "swift test". After running the test, it will report

◇ Test example() started.
✘ Test example() recorded an issue at testfailTests.swift:5:2: Caught error: whatever
✘ Test example() failed after 0.001 seconds with 1 issue.
✘ Test run with 1 test failed after 0.001 seconds with 1 issue.

Where testfailTests.swift:5:2 is the location of the start of the function whereas it should have reported line 8 since that's where the throw occurred.

Expected behavior

The testing framework should report the line where the throw occurred, not the start of the function.

Environment

↳ Testing Library Version: 1055

% swift --version swift-driver version: 1.124.5 Apple Swift version 6.2 (swiftlang-6.2.0.5.56 clang-1700.3.5.51) Target: arm64-apple-macosx15.0

% uname -a Darwin MacBook-Pro-1800.local 24.4.0 Darwin Kernel Version 24.4.0: Wed Mar 19 21:37:10 PDT 2025; root:xnu_development-11417.101.15~1/DEVELOPMENT_ARM64_T6000 arm64

Additional information

No response

bstecher1 avatar Apr 16 '25 18:04 bstecher1

Source location information is not directly available for thrown errors as the Swift runtime doesn't gather that sort of information. The closest available source location information in this case is that of the test declaration. Swift Testing gathers a backtrace for any thrown error in a test and tools may be able to use this backtrace to gather more precise source location information. Xcode uses this backtrace to provide fine-grained test failure callouts in the Source Editor, for instance.

grynspan avatar Apr 19 '25 19:04 grynspan

Closed prematurely as we would still like to track the issue, even if we don't expect a solution in the near term.

grynspan avatar Apr 21 '25 19:04 grynspan