質問

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.

他のヒント

As the error message points out, the problem is that you're trying to present on a UIViewController whose view is not in the UIWindow hierarchy.

The easiest way to fix it:

- (void)testExample {

    //
    // Arrange
    // Storyboard
    //
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

    //
    // Arrange
    // View Controller
    //
    UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
    [UIApplication sharedApplication].keyWindow.rootViewController = viewController;

    //
    // Act
    //
    [viewController performSegueWithIdentifier:@"ModalSegue" sender:nil];

    //
    // Assert
    //
    XCTAssertEqualObjects(viewController.presentedViewController.title, @"Second");

}

Unit tests (i.e. those you get when adding unit tests to a project via the Xcode template) are not for testing your UI. You need to look at UI Automation which allows you to code up UI actions using JavaScript.

Check out KIF. It sounds like pretty much what you need:

https://github.com/kif-framework/KIF

It's an Objective-C based UI Testing framework that integrates with the Accessibility framework built in to the app (as UI Automation does).

Or follow this tutorial to ensure it is what you want:

http://www.raywenderlich.com/61419/ios-ui-testing-with-kif

Hope you find your solution!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top