Skip to content

Diagnose issues recorded "late", after their associated test has finished #1283

@stmontgomery

Description

@stmontgomery

Description

It is currently possible for an Issue in a Swift Testing test to be recorded “late”, after the test it's associated with has officially finished. This issue tracks an enhancement which would explain this situation clearly when it happens, to help users understand and modify their test to avoid it.

One common way this can occur is when a test uses an unstructured Task and does not await its value to ensure it finishes before the @Test function returns. Here's an example:

@Test func example() async throws {
  Task {
    try await Task.sleep(for: .seconds(1))
    Issue.record("Late")
  }
  // Didn't 'await' that Task!
}

@Test func longRunning() async throws {
  try await Task.sleep(for: .seconds(5))
}

This outputs the following:

◇ Test longRunning() started.
◇ Test example() started.
✔ Test example() passed after 0.001 seconds.
✘ Test example() recorded an issue at Example.swift:6:17: Issue recorded
↳ Late
✔ Test longRunning() passed after 5.194 seconds.
✘ Test run with 2 tests in 0 suites failed after 5.194 seconds with 1 issue.

This can be a confusing experience: it says example() passed, and yet later it claims example() recorded an issue. This could be resolved by modifying example() as follows:

 @Test func example() async throws {
-  Task {
+  await Task {
     try await Task.sleep(for: .seconds(1))
     Issue.record("Late")
-  }
+  }.value
 }

This situation is ultimately the responsibility of the test author to resolve, generally by ensuring all issues are recorded before the test function returns. But when this problem happens, the testing library could help clarify what happened by diagnosing this with an extra note in the console message.

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    command-line experience⌨️ enhancements to the command line interfaceenhancementNew feature or requestissue-handlingRelated to Issue handling within the testing library

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions