---
title: "runActivity(named:block:)"
framework: xctest
role: symbol
role_heading: Type Method
path: "xctest/xctcontext/runactivity(named:block:)"
---

# runActivity(named:block:)

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

## Declaration

```swift
@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

Return Value A Result object that indicates whether the block of code runs successfully or encounters an error.

## Discussion

Discussion Run a block of code as a named substep in a test. For more information, see Grouping Tests into Substeps with Activities. To save screenshots or other test-result data for later investigation, call the add(_:) method on the instance of XCTActivity that the test system passes to your block. For more information, see Adding Attachments to Tests, Activities, and Issues.

## See Also

### Related Documentation

- [Grouping Tests into Substeps with Activities](xctest/grouping-tests-into-substeps-with-activities.md)
- [Adding Attachments to Tests, Activities, and Issues](xctest/adding-attachments-to-tests-activities-and-issues.md)
