Skip to content

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

@bstecher1

Description

@bstecher1

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

Activity

grynspan

grynspan commented on Apr 19, 2025

@grynspan
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.

added
wontfix❌ This will not be worked on
and removed
bug🪲 Something isn't working
on Apr 19, 2025
grynspan

grynspan commented on Apr 21, 2025

@grynspan
Contributor

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

added
enhancementNew feature or request
issue-handlingRelated to Issue handling within the testing library
and removed
wontfix❌ This will not be worked on
on Apr 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestissue-handlingRelated to Issue handling within the testing library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @grynspan@bstecher1

        Issue actions

          thrown error from a naked try (not inside #expect/#require) from a test does not report correct line number · Issue #1079 · swiftlang/swift-testing