Skip to content

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

Open
@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

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

    Issue actions