11

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.

Community
  • 1
  • 1
FreeNickname
  • 7,398
  • 2
  • 30
  • 60
  • **If you think you've seen this before:** I've already asked [a question with a very-very similar text](http://stackoverflow.com/questions/35506605/why-should-i-use-a-separate-test-target-for-running-xctests-and-how-should-i-do). But because of my mistake it was about a separate test target, not about a separate test host. If you're wondering why I kept that question and created a new one, [here's a meta question explaining my reasons](http://meta.stackoverflow.com/questions/317294/i-was-given-a-good-answer-but-then-noticed-that-the-question-is-wrong-what-sh#317294). – FreeNickname Feb 19 '16 at 22:12
  • I'd suggest doing some research. There's an awful lot about this on the Interwebs... – matt Feb 19 '16 at 22:19
  • 1
    @matt, I always try to find an answer myself first, but this time I didn't succeed, unfortunately. It's hard to find information specifically about using a separate test host. Most articles focus on ways to add unit tests to an existing project, on different functions available in XCTests (Equal, EqualObjects, etc.) and everything else. That's why I asked for help here. – FreeNickname Feb 20 '16 at 08:22

1 Answers1

11

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

Jon Reid
  • 20,545
  • 2
  • 64
  • 95
  • Using a separate app delegate makes sense, and this way I don't have to keep two hosts (the main app and the test host) in sync. Thank you! – FreeNickname Feb 20 '16 at 10:28