Skip to content

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

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
bstecher1 opened this issue Apr 16, 2025 · 2 comments
Labels
enhancement New feature or request issue-handling Related to Issue handling within the testing library

Comments

@bstecher1
Copy link

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 bstecher1 added bug 🪲 Something isn't working triage-needed labels Apr 16, 2025
@grynspan
Copy link
Contributor

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 grynspan closed this as not planned Won't fix, can't repro, duplicate, stale Apr 19, 2025
@grynspan grynspan added wontfix ❌ This will not be worked on and removed bug 🪲 Something isn't working triage-needed labels Apr 19, 2025
@grynspan grynspan reopened this Apr 21, 2025
@grynspan
Copy link
Contributor

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

@grynspan grynspan added enhancement New feature or request issue-handling Related to Issue handling within the testing library and removed wontfix ❌ This will not be worked on labels Apr 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request issue-handling Related to Issue handling within the testing library
Projects
None yet
Development

No branches or pull requests

2 participants