Search code examples
iosobjective-cunit-testingxctestxcode7.1

iOS XCtest accessing App View Controllers/UI


I am new to iOS development. I want to write unit tests for an app which uses an SDK where the authenticate method is of the form:

(void)authenticate:(UIViewController *)presentingViewController clearCookies:(BOOL)clearCookies completionBlock:(AuthCompletionBlock)completionBlock.

To authenticate the user, an embedded web browser needs to open in the UIViewController(passed in the method parameters) . Can the unit tests access app UI? How do I make sure that the browser opens, user authenticates thru app UI and then the unit tests execute.


Solution

  • Depends if you want unit tests or UI tests.

    • Unit tests: Fast. Consistent. They confirm step-by-step. But they don't go end-to-end.
    • UI tests: Slow. Fragile. They confirm end-to-end.

    For unit testing, you wouldn't write tests that actually bring up a browser or interact with it in any way. Instead, you'd write tests that would bring up the browser, and tests that simulate different inputs returned from the browser.

    This works as long as you're confident in the back-and-forth communication. If so, there's no need to test Apple's code. If not, then you can write a spike solution to understand the communication.