Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I use a separate test host for running XCTests and how should I do that?

I once asked a question related to XCTests. And in one of the answers I was told that it is a common practice to use a separate test host (other than the main app) when running unit tests (at least, in iOS development). I tried to find some sources about it, but I couldn't

I understand, that it is probably a best practice, so I would really like to understand it. Could someone explain to me why is it important, what benefits do I get from it and how should I go about doing it? Links to some articles explaining the issue will be much appreciated.

P.S. I understand that I need special environment for tests (fake in-memory database, mocked networking layer, etc.), but up until now I managed to achieve it without a separate test host, using just my main app, like the Xcode suggests by default. But I believe that there might be a better way. And I know that the defaults that Xcode suggests are not always that great.

like image 766
FreeNickname Avatar asked Feb 19 '16 21:02

FreeNickname


1 Answers

Reasons to use a different host app for unit tests:

  1. There is no main app, because your target is a library.
  2. When the main app is launched, it goes through its start-up process. This takes too long, and has side-effects.

Reason 1 is pretty much a given. But what about reason 2?

My approach is to use the main app, but use a different application delegate during testing. That way I don't have to maintain a separate app, just an alternate start-up process. See How to Easily Switch Your App Delegate for Testing

like image 73
Jon Reid Avatar answered Sep 27 '22 21:09

Jon Reid