1

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.

Jamie Eltringham
  • 810
  • 3
  • 16
  • 25
SKFelix
  • 13
  • 3
  • For that you should use mock data to feed into your authentication method. The unit tests do not access ui. There is the actual UI tests though, maybe look into that. – hola Mar 01 '16 at 08:52
  • Agree with hola. Take a look on recording UI tests. Process is really simple and smooth. Take a look on it in action: https://developer.apple.com/videos/play/wwdc2015/406/ – Aleksei Minaev Mar 01 '16 at 09:00

1 Answers1

0

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.

Jon Reid
  • 20,545
  • 2
  • 64
  • 95
  • I actually need to write unit tests but for logging in I would need to interact with the browser right? Is there a non UI related way of interacting with the browser (entering user creds and loggin in)? Xcode UI tests seem to be quite limited for functional tests. – SKFelix Mar 01 '16 at 18:57
  • What I mean is, unit tests wouldn't _actually_ do logging in via a web page. You'd simulate starting it, and ending it. – Jon Reid Mar 01 '16 at 19:01
  • What is the suggested method of going about it? How sensible is it to use an external framework for this? say OCMock? – SKFelix Mar 02 '16 at 12:51
  • Your choice of mocking tool doesn't matter nearly as much as writing your code to be able to inject those mocks. You can learn more by looking up Dependency Injection. I gave a talk http://qualitycoding.org/dependency-injection/ – Jon Reid Mar 02 '16 at 16:08