In XCode 7 direct support for two types of tests has been added:
- Unit tests: These tests can access your project’s classes and are directly linked to your project. They are used for low-level tests where you test the functions of your classes. Usually one test class for one project class is created.
- UI tests: These tests run independently of your project and perform UI actions. I.e. they cannot access the classes or data of your app. But they can use the app like a user, i.e. they can interact with it, tap buttons, enter text etc.
To add such tests to your existing project just choose “New > Target” in XCode 7:
It will create example classes that make it easier to start. To create a new UI test is also simple. Just create an empty function, e.g.
- (void)testEverything { }
Then place the cursor in this function an click on the record button to start the simulator and record your actions as commands into the method:
To run the tests when you are finished just long click on the play button in XCode and select “Test”.
Snippets for UI test
Turning a switch on:
if ([tablesQuery.switches[@"My switch label"].value isEqualToString:@"0"]) { [tablesQuery.switches[@"My switch label"] tap]; }
Checking the position of a row in a table:
XCTAssertLessThanOrEqual([[app.staticTexts[@"My row label"] coordinateWithNormalizedOffset:CGVectorMake(0, 0)] screenPoint].y, 90);
Searching for a table row that starts with “Until:” (you can find a reference of the BEGINSWITH and similar commands here):
[[app.staticTexts elementMatchingPredicate:[NSPredicate predicateWithFormat:@"label BEGINSWITH 'Until:'"]] tap];
Displaying all currently visible elements:
NSLog(app.debugDescription);
Overview of the available functions: