Add test cases and test methods to a test target to confirm that your code behaves as expected.
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
To add tests to your project:
Create a new subclass of
XCTestwithin 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
XCTest 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.
This example defines an
Table, with a single test method,
test. This test method creates a new instance of a class called
Table, and checks that its
column 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, and thrown errors.
For example, Listing 1 above uses the XCTAssertEqual(_:_:_:file:line:) function to assert that two integers have the same value.