XCTestCase Class Reference
Inherits from | XCTest : NSObject |
---|---|
Declared in | XCTestCase.h |
Overview
- @class XCTestCase
- XCTestCase is a concrete subclass of XCTest that should be the override point for
- most developers creating tests for their projects. A test case subclass can have
- multiple test methods and supports setup and tear down that executes for every test
- method as well as class level setup and tear down. *
- To define a test case: *
- • Create a subclass of XCTestCase.
- • Implement -test methods.
- • Optionally define instance variables or properties that store the state of the test.
- • Optionally initialize state by overriding setUp
- • Optionally clean-up after a test by overriding tearDown. *
- Test methods are instance methods meeting these requirements:
- • accepting no parameters
- • returning no value
- • prefixed with ‘test’ *
For example:
- (void)testSomething;
Test methods are automatically recognized as test cases by the XCTest framework.
- Each XCTestCase subclass’s defaultTestSuite is a XCTestSuite which includes these
- tests. Test method implementations usually contain assertions that must be verified
for the test to pass, for example:
@interface MathTest : XCTestCase { @private float f1; float f2; }
- (void)testAddition;
@end
@implementation MathTest
(void)setUp { f1 = 2.0; f2 = 3.0; }
(void)testAddition { XCTAssertTrue (f1 + f2 == 5.0); } @end
@category AsynchronousTesting
@discussion This category introduces support for asynchronous testing in XCTestCase. The mechanism allows you to specify one or more “expectations” that will occur asynchronously as a result of actions in the test. Once all expectations have been set, a “wait” API is called that will block execution of subsequent test code until all expected conditions have been fulfilled or a timeout occurs.
Other Methods
+ testCaseWithInvocation:
@method +testCaseWithInvocation:
+ (instancetype)testCaseWithInvocation:(nullable NSInvocation *)invocation
Declared In
XCTestCase.h
– initWithInvocation:
@method -initWithInvocation:
- (instancetype)initWithInvocation:(nullable NSInvocation *)invocation
Declared In
XCTestCase.h
+ testCaseWithSelector:
@method +testCaseWithSelector:
+ (instancetype)testCaseWithSelector:(SEL)selector
Declared In
XCTestCase.h
– initWithSelector:
@method -initWithSelector:
- (instancetype)initWithSelector:(SEL)selector
Declared In
XCTestCase.h
invocation
@property invocation The invocation used when this test is run.
@property (strong, nullable) NSInvocation *invocation
Declared In
XCTestCase.h
– invokeTest
@method -invokeTest Invoking a test performs its setUp, invocation, and tearDown. In general this should not be called directly.
- (void)invokeTest
Declared In
XCTestCase.h
continueAfterFailure
@property continueAfterFailure The test case behavior after a failure. Defaults to YES.
@property BOOL continueAfterFailure
Declared In
XCTestCase.h
– recordFailureWithDescription:inFile:atLine:expected:
@method -recordFailureWithDescription:inFile:atLine:expected: Records a failure in the execution of the test and is used by all test assertions.
- (void)recordFailureWithDescription:(NSString *)description inFile:(NSString *)filePath atLine:(NSUInteger)lineNumber expected:(BOOL)expected
Parameters
description |
The description of the failure being reported. |
---|---|
filePath |
The file path to the source file where the failure being reported was encountered. |
lineNumber |
The line number in the source file at filePath where the failure being reported was encountered. |
expected |
YES if the failure being reported was the result of a failed assertion, NO if it was the result of an uncaught exception. |
Declared In
XCTestCase.h
+ testInvocations
@method +testInvocations Invocations for each test method in the test case.
+ (NSArray<NSInvocation*> *)testInvocations
Declared In
XCTestCase.h
+ defaultPerformanceMetrics
@method +defaultPerformanceMetrics The names of the performance metrics to measure when invoking measureBlock:. Returns XCTPerformanceMetric_WallClockTime by default. Subclasses can override this to change the behavior of measureBlock:
+ (NSArray<NSString*> *)defaultPerformanceMetrics
Declared In
XCTestCase.h
– measureBlock:
- @method -measureBlock: *
- Call from a test method to measure resources (defaultPerformanceMetrics) used by the
- block in the current process.
- (void)measureBlock:(void ( ^ ) ( void ))block
Discussion
- (void)testPerformanceOfMyFunction {
[self measureBlock:^{
// Do that thing you want to measure.
MyFunction();
}];
}
- @param block A block whose performance to measure.
Declared In
XCTestCase.h
– measureMetrics:automaticallyStartMeasuring:forBlock:
- @method -measureMetrics:automaticallyStartMeasuring:forBlock: *
- Call from a test method to measure resources (XCTPerformanceMetrics) used by the
- block in the current process. Each metric will be measured across calls to the block.
- The number of times the block will be called is undefined and may change in the
- future. For one example of why, as long as the requested performance metrics do
- not interfere with each other the API will measure all metrics across the same
- calls to the block. If the performance metrics may interfere the API will measure
- them separately.
- (void)measureMetrics:(NSArray<NSString*> *)metrics automaticallyStartMeasuring:(BOOL)automaticallyStartMeasuring forBlock:(void ( ^ ) ( void ))block
Discussion
- (void)testMyFunction2_WallClockTime {
[self measureMetrics:[[self class] defaultPerformanceMetrics] automaticallyStartMeasuring:NO forBlock:^{
// Do setup work that needs to be done for every iteration but you don't want to measure before the call to -startMeasuring
SetupSomething();
[self startMeasuring];
// Do that thing you want to measure.
MyFunction();
[self stopMeasuring];
// Do teardown work that needs to be done for every iteration but you don't want to measure after the call to stopMeasuring
TeardownSomething();
}];
}
- Caveats:
- • If YES was passed for automaticallyStartMeasuring and startMeasuring is called
- anyway, the test will fail.
- • If NO was passed for automaticallyStartMeasuring then startMeasuring must be
- called once and only once before the end of the block or the test will fail.
- • If stopMeasuring is called multiple times during the block the test will fail. *
- @param metrics An array of NSStrings (XCTPerformanceMetrics) to measure. Providing an unrecognized string is a test failure. *
- @param automaticallyStartMeasuring If NO, XCTestCase will not take any measurements until startMeasuring is called. *
- @param block A block whose performance to measure.
Declared In
XCTestCase.h
– startMeasuring
@method -startMeasuring Call this from within a measure block to set the beginning of the critical section. Measurement of metrics will start at this point.
- (void)startMeasuring
Declared In
XCTestCase.h
– stopMeasuring
@method -stopMeasuring Call this from within a measure block to set the ending of the critical section. Measurement of metrics will stop at this point.
- (void)stopMeasuring
Declared In
XCTestCase.h
– addUIInterruptionMonitorWithDescription:handler:
Adds a handler to the current context. Returns a token that can be used to unregister the handler. Handlers are invoked in the reverse order in which they are added until one of the handlers returns true, indicating that it has handled the alert.
- (id<NSObject>)addUIInterruptionMonitorWithDescription:(NSString *)handlerDescription handler:(BOOL ( ^ ) ( XCUIElement *interruptingElement ))handler
Parameters
handlerDescription |
Explanation of the behavior and purpose of this handler, mainly used for debugging and analysis. |
---|---|
handler |
Handler block for asynchronous UI such as alerts and other dialogs. Handlers should return true if they handled the UI, false if they did not. The handler is passed an XCUIElement representing the top level UI element for the alert. |
Declared In
XCTestCase.h
– removeUIInterruptionMonitor:
Removes a handler using the token provided when it was added.
- (void)removeUIInterruptionMonitor:(id<NSObject>)monitor
Declared In
XCTestCase.h
XCTestSuiteExtensions Methods
+ defaultTestSuite
@method +defaultTestSuite Returns a test suite containing test cases for all of the tests in the class.
+ (XCTestSuite *)defaultTestSuite
Declared In
XCTestCase.h
+ setUp
@method +setUp Setup method called before the invocation of any test method in the class.
+ (void)setUp
Declared In
XCTestCase.h
+ tearDown
@method +testDown Teardown method called after the invocation of every test method in the class.
+ (void)tearDown
Declared In
XCTestCase.h
AsynchronousTesting Methods
– expectationWithDescription:
@method +expectationWithDescription:
- (XCTestExpectation *)expectationWithDescription:(NSString *)description
Discussion
@param description This string will be displayed in the test log to help diagnose failures.
@discussion Creates and returns an expectation associated with the test case.
Declared In
XCTestCase+AsynchronousTesting.h
– waitForExpectationsWithTimeout:handler:
@method -waitForExpectationsWithTimeout:handler:
- (void)waitForExpectationsWithTimeout:(NSTimeInterval)timeout handler:(nullable XCWaitCompletionHandler)handler
Discussion
@param timeout The amount of time within which all expectations must be fulfilled.
@param handler If provided, the handler will be invoked both on timeout or fulfillment of all expectations. Timeout is always treated as a test failure.
@discussion -waitForExpectationsWithTimeout:handler: creates a point of synchronization in the flow of a test. Only one -waitForExpectationsWithTimeout:handler: can be active at any given time, but multiple discrete sequences of { expectations -> wait } can be chained together.
-waitForExpectationsWithTimeout:handler: runs the run loop while handling events until all expectations are fulfilled or the timeout is reached. Clients should not manipulate the run loop while using this API.
Declared In
XCTestCase+AsynchronousTesting.h
– keyValueObservingExpectationForObject:keyPath:expectedValue:
@method keyValueObservingExpectationForObject:keyPath:expectedValue:
- (XCTestExpectation *)keyValueObservingExpectationForObject:(id)objectToObserve keyPath:(NSString *)keyPath expectedValue:(nullable id)expectedValue
Discussion
@discussion A convenience method for asynchronous tests that use Key Value Observing to detect changes to values on an object. This variant takes an expected value and observes changes on the object until the keyPath’s value matches the expected value using [NSObject isEqual:]. If other comparisions are needed, use the variant below that takes a handler block.
@param objectToObserve The object to observe.
@param keyPath The key path to observe.
@param expectedValue Expected value of the keyPath for the object. The expectation will fulfill itself when the keyPath is equal, as tested using [NSObject isEqual:]. If nil, the expectation will be fulfilled by the first change to the key path of the observed object.
@return Creates and returns an expectation associated with the test case.
Declared In
XCTestCase+AsynchronousTesting.h
– keyValueObservingExpectationForObject:keyPath:handler:
@method keyValueObservingExpectationForObject:keyPath:handler:
- (XCTestExpectation *)keyValueObservingExpectationForObject:(id)objectToObserve keyPath:(NSString *)keyPath handler:(nullable XCKeyValueObservingExpectationHandler)handler
Discussion
@discussion Variant of the convenience for tests that use Key Value Observing. Takes a handler block instead of an expected value. Every KVO change will run the handler block until it returns YES (or the wait times out). Returning YES from the block will fulfill the expectation. XCTAssert and related APIs can be used in the block to report a failure.
@param objectToObserve The object to observe.
@param keyPath The key path to observe.
@param handler Optional handler, /see XCKeyValueObservingExpectationHandler. If not provided, the expectation will be fulfilled by the first change to the key path of the observed object.
@return Creates and returns an expectation associated with the test case.
Declared In
XCTestCase+AsynchronousTesting.h
– expectationForNotification:object:handler:
@method expectationForNotification:object:handler:
- (XCTestExpectation *)expectationForNotification:(NSString *)notificationName object:(nullable id)objectToObserve handler:(nullable XCNotificationExpectationHandler)handler
Discussion
@discussion A convenience method for asynchronous tests that observe NSNotifications.
@param notificationName The notification to register for.
@param objectToObserve The object to observe.
@param handler Optional handler, /see XCNotificationExpectationHandler. If not provided, the expectation will be fulfilled by the first notification matching the specified name from the observed object.
@return Creates and returns an expectation associated with the test case.
Declared In
XCTestCase+AsynchronousTesting.h
– expectationForPredicate:evaluatedWithObject:handler:
@method expectationForPredicate:evaluatedWithObject:handler: Creates an expectation that is fulfilled if the predicate returns true when evaluated with the given object. The expectation periodically evaluates the predicate and also may use notifications or other events to optimistically re-evaluate.
- (XCTestExpectation *)expectationForPredicate:(NSPredicate *)predicate evaluatedWithObject:(id)object handler:(nullable XCPredicateExpectationHandler)handler
Declared In
XCTestCase+AsynchronousTesting.h