Class

XCTestCase

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

Declaration

@interface XCTestCase : XCTest

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.

+ setUp

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

- addTeardownBlock:

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

+ tearDown

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

Handling Test Case Failure

continueAfterFailure

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

Measuring Performance

- measureBlock:

Measures the performance of a block of code.

- measureMetrics:automaticallyStartMeasuring:forBlock:

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

- startMeasuring

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

- stopMeasuring

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

defaultPerformanceMetrics

Identifies the performance metrics measured when measureBlock: is invoked.

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).

- expectationWithDescription:

Creates a new expectation with an associated description.

- expectationForPredicate:evaluatedWithObject:handler:

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

- expectationForNotification:object:handler:

Creates an expectation that is fulfilled when a specific NSNotification is received for a given object.

- keyValueObservingExpectationForObject:keyPath:expectedValue:

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

- keyValueObservingExpectationForObject:keyPath:handler:

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.

- waitForExpectations:timeout:

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

- waitForExpectations:timeout:enforceOrder:

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

- waitForExpectationsWithTimeout:handler:

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

XCWaitCompletionHandler

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

XCTestErrorCode

Error codes for errors that can occur while waiting for expectations to be fulfilled.

XCTestErrorDomain

Error domain for errors that can occur while waiting for expectations to be fulfilled.

Monitoring UI Interruptions

Handling UI Interruptions

Improve your UI test’s stability by handling interface changes that block the UI elements under test.

- addUIInterruptionMonitorWithDescription:handler:

Adds a handler to the current context.

- removeUIInterruptionMonitor:

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.

testInvocations

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

invocation

The invocation used when this test is run.

- invokeTest

Invokes the test.

- recordFailureWithDescription:inFile:atLine:expected:

Records a failure in the execution of the test.

defaultTestSuite

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.

XCTest

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