Contents

Defining Test Cases and Test Methods

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

Overview

Add tests to your Xcode project by writing one or more test methods, each of which verifies a specific aspect of your code. Group related test methods into test cases, each of which is a subclass of XCTestCase.

To add tests to your project:

  • Create a new subclass of XCTestCase within a test target.

  • Add one or more test methods to the test case.

  • Add one or more test assertions to each test method.

A test method is an instance method on an XCTestCase subclass, with no parameters, no return value, and a name that begins with the lowercase word test. Test methods are automatically detected by the XCTest framework in Xcode.

Listing 1. Example test case and test method

This example defines an XCTestCase subclass, TableValidationTests, with a single test method, testEmptyTableRowAndColumnCount(). This test method creates a new instance of a class called Table, and checks that its rowCount and columnCount properties are both equal to 0 after initialization.

Asserting Test Conditions

You can check (or assert) conditions inside test methods to make sure that your code is behaving as expected. Use the XCTAssert family of functions to check for Boolean conditions, nil or non-nil values, expected values, errors, or thrown exceptions.

For example, Listing 1 above uses the XCTAssertEqual macro to assert that two integers have the same value.

See Also

Test cases and test methods