Grouping Tests into Substeps with Activities

Create named activities within test methods to simplify test reports.


Use activities to break longer test methods, such as UI tests and integration tests, into smaller named substeps. For example, break a complex UI login test into three substeps named "Open login window", "Enter password", and "Close login window", each of which contains the checks and assertions appropriate to that substep.

Each activity wraps a block of code, giving the code a name. These names are used to group the activity's test output in Xcode test reports, making reports easier to understand for complex multi-step tests. You can nest and call activities within other activities to an arbitrary depth.

Activities run against the current testing context, represented by XCTContext. To run a block of code as an activity, call the runActivityNamed:block: class method on XCTContext, passing your test code as the block to execute.

Use activities to convert common test substeps into self-contained utility methods for reuse in multiple tests. For example, if you have three UI tests that each require the user to be logged in, extract the login process into a utility method that wraps the process inside an activity called "Login", and call the utility method from within each test method. The login activity will appear in the Xcode test report for each test method that calls it.

You can use XCTContext anywhere within your test target, not just within test methods on an XCTestCase subclass. This enables you to define activities in your own utility code, such as in custom methods on subclasses of XCUIApplication or XCUIElement.

See Also


class XCTContext

A proxy for the current testing context.

protocol XCTActivity

A named substep of a test method.