Class

XCTestCase

The primary class for defining test cases, test methods, and performance tests.

Overview

A test case is a group of related test methods, with optional setup and teardown before and after tests are run. See Defining Test Cases and Test Methods for more information.

Topics

Customizing Test Setup and Teardown

Understanding Setup and Teardown for Test Methods

Prepare initial state before tests run and perform cleanup after tests complete.

class func setUp()

Provides an opportunity to customize initial state before a test case begins.

func addTeardownBlock(() -> Void)

Registers a block of teardown code to be run after the current test method ends.

class func tearDown()

Provides an opportunity to perform cleanup after a test case ends.

Handling Test Case Failure

var continueAfterFailure: Bool

A Boolean value indicating whether the test case should continue running after a failure occurs.

Measuring Performance

func measure(() -> Void)

Measures the performance of a block of code.

func measureMetrics([XCTPerformanceMetric], automaticallyStartMeasuring: Bool, for: () -> Void)

Measures the performance of a block of code, optionally deferring the starting point for measurement.

func startMeasuring()

Starts the measurement of performance metrics within a block of code.

func stopMeasuring()

Ends the measurement of performance metrics within a block of code.

class var defaultPerformanceMetrics: [XCTPerformanceMetric]

Identifies the performance metrics measured when measure(_:) is invoked.

struct XCTPerformanceMetric

Performance metrics that can be measured by XCTest.

Creating Asynchronous Test Expectations

To create asynchronous test expectations, use the convenience methods below, or create instances of XCTestExpectation and its subclasses manually (as described in Asynchronous Tests and Expectations).

func expectation(description: String)

Creates a new expectation with an associated description.

func expectation(for: NSPredicate, evaluatedWith: Any, handler: XCTNSPredicateExpectation.Handler? = nil)

Creates an expectation that is fulfilled if the predicate returns true when evaluated with the given object.

func keyValueObservingExpectation(for: Any, keyPath: String, expectedValue: Any?)

Creates an expectation that uses Key Value Observing to observe a value until it matches an expected value.

func keyValueObservingExpectation(for: Any, keyPath: String, handler: XCTKVOExpectation.Handler? = nil)

Creates an expectation that uses Key Value Observing to observe a value and respond to changes in that value by calling a provided handler.

Waiting for Expectations

To wait for asynchronous test expectations to be fulfilled, use the convenience methods below, or create an instance of XCTWaiter.

func wait(for: [XCTestExpectation], timeout: TimeInterval)

Waits on a group of expectations for up to the specified timeout.

func wait(for: [XCTestExpectation], timeout: TimeInterval, enforceOrder: Bool)

Waits on an array of expectations and specifies whether they must be fulfilled in the given order.

func waitForExpectations(timeout: TimeInterval, handler: XCWaitCompletionHandler? = nil)

Waits until all expectations are fulfilled or the timeout is reached.

typealias XCWaitCompletionHandler

A block to be called when a call to waitForExpectations(timeout:handler:) has all of its expectations fulfilled, or times out.

Monitoring UI Interruptions

func removeUIInterruptionMonitor(NSObjectProtocol)

Removes a handler using the token provided when it was added.

Creating Tests Programmatically

Test methods you define in subclasses of XCTestCase are detected automatically by the XCTest framework. Use the symbols below if you need more customization for test case creation, such as to define test cases that cannot be known at compile time.

class var testInvocations: [NSInvocation]

Returns an array of invocations representing each test method in the test case.

var invocation: NSInvocation?

The invocation used when this test is run.

func invokeTest()

Invokes the test.

class var defaultTestSuite: XCTestSuite

A test suite containing test cases for all of the tests in the class.

Relationships

Inherits From

See Also

Test Cases and Test Methods

Defining Test Cases and Test Methods

Add test cases and test methods to a test target to confirm that your code behaves as expected.

class XCTest

An abstract base class for creating, managing, and executing tests.