I have an iOS app and I want to test its UI from code (with unit-test). I want to imitate a user's activity and storyboard activity: tap on button, tap on barbutton, write to textfield, perform segue.
I have problem with seques. I have a TableViewConrtoller in a NavigationController. Firstly, how can i reach the TableViewController from code?
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
UINavigationController *nav = [storyboard instantiateInitialViewController];
GoodHabitsViewController *good = [nav.viewControllers objectAtIndex:0];
NSLog(@"%@", good.addButton.title);
[good performSegueWithIdentifier:@"AddGoodHabitSegue" sender:good];
NSLog write correct value, the button's title is "add", and "add" appears in log. But after the performSequeWithIdentifier the following warning appears:
Warning: Attempt to present <AddGoodHabitViewController: 0x74c8310> on <UINavigationController: 0x764e120> whose view is not in the window hierarchy!
How can I perform the seque, how can I push addButton (UIBarButton) and how can I make reference to AddGoodHabitViewController
(where the segue point).
There are two varieties of tests that can be run on the OCUnit platform - so called "application" tests, and so-called "logic" tests.
Only "application" tests allow you to exercise UIViewControllers, UIViews and so forth. If you specified "create test target" when you created the project, it will use the "application" style. Otherwise adding a teset target later, by default, uses the "logic" style. To covert an existing test target to use "application" tests, TwoBit Labs have a nice: guide
Another way to exercise code all the way up to the view controller and views is to use the Cedar BDD test framework. Cedar tests are run inside of an iOS application, so besides being able to test ViewControllers and Views, its also possible to test on either device or simulator.
UIAutomation allows the execution of automated functional tests, by driving the UI itself (as opposed to exercising UI code). The problem I have with UIAutomation is that, as far as I know, its not possible to execute it from the cmd-line, making it hard to include in an automated test suite - one that would be run by a continuous integration server. . . . someone might come up with a work-around for this using Automator.app or similar, but so far nobody has.
Calabash is another great UI testing framework, and can be run from the cmd-line, so doesn't suffer from the limitations I described above, wrt UIAutomation.
Bear in mind that automated functional testing, and testing UIViewControllers and Views at the code-level are two very different things. The latter certainly has value, and simply requires setting up the bundle loaders correctly.
Update: With recent versions of Xcode 5+, application-style tests are the default.