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

徘徊边缘 提交于 2019-12-01 05:02:59

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/35515825/why-should-i-use-a-separate-test-host-for-running-xctests-and-how-should-i-do-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!