<!--
{
  "availability" : [

  ],
  "documentType" : "symbol",
  "framework" : "XCTest",
  "identifier" : "/documentation/XCTest/XCTContext/runActivity(named:block:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "XCTest"
    ],
    "preciseIdentifier" : "s:So10XCTContextC6XCTestE11runActivity5named5blockxSS_xSo11XCTActivity_pKXEtKlFZ"
  },
  "title" : "runActivity(named:block:)"
}
-->

# runActivity(named:block:)

Creates and runs an activity with the provided block of code.

```
@MainActor @preconcurrency class func runActivity<Result>(named name: String, block: @MainActor (any XCTActivity) throws -> Result) rethrows -> Result
```

## Parameters

`name`

A descriptive name for the activity, for display in Xcode’s test results browser.

`block`

A block of code for the test to execute as the body of the activity.

## Return Value

A <doc://com.apple.documentation/documentation/Swift/Result> object that indicates whether the block of code runs successfully or encounters an error.

## Discussion

Run a block of code as a named substep in a test. For more information, see [Grouping Tests into Substeps with Activities](/documentation/XCTest/grouping-tests-into-substeps-with-activities).

To save screenshots or other test-result data for later investigation, call the [`add(_:)`](/documentation/XCTest/XCTActivity/add(_:)) method on the instance of [`XCTActivity`](/documentation/XCTest/XCTActivity) that the test system passes to your block. For more information, see [Adding Attachments to Tests, Activities, and Issues](/documentation/XCTest/adding-attachments-to-tests-activities-and-issues).

## See Also

[Grouping Tests into Substeps with Activities](/documentation/XCTest/grouping-tests-into-substeps-with-activities)

Simplify test reports by creating activities that organize substeps within complex test methods.

[Adding Attachments to Tests, Activities, and Issues](/documentation/XCTest/adding-attachments-to-tests-activities-and-issues)

Use attachments to store a test’s output data for later analysis.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)