0

I recently started to write tests for my project. after writing a few amount of tests i realized in coverage section some of my classes or functions has coverage more than 50%(like didFinishLaunchingWithOptions func in AppDelegate) without that i wrote any test for them. where this coverage comes from?

Mkhakpaki
  • 120
  • 2
  • 12

1 Answers1

2

When the tests are running, they are starting your app. That means they are using your UIApplicationDelegate implementation and didFinishLaunchingWithOptions runs, including all methods that are called from it.

If a method runs, its coverage is calculated. If there is no branching, the coverage will be 100%.

Related: Unit Testing in Xcode, does it run the app? Basically, you can inject an empty application delegate when testing to avoid this kind of behavior.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • so there is same logic for functions like viewDidAppear in view controller? – Mkhakpaki Jun 19 '18 at 09:11
  • @MohammadKhakpaki . I don't get your question. I am telling you that unit tests basically start you app and then they are running the unit tests inside it. – Sulthan Jun 19 '18 at 09:14
  • i understood you. my next question is this situation(100% coverage ) is also happening for functions like viewDidAppear in viewControllers. Do you think this behavior reason is same as above that you told? – Mkhakpaki Jun 19 '18 at 09:27
  • @MohammadKhakpaki Yes. The app delegate normally instantiates the root controller and shows it on the screen. It's the same as when you start your app normally but you don't touch the screen at all. – Sulthan Jun 19 '18 at 09:31
  • I use a different app delegate when running tests: https://qualitycoding.org/ios-app-delegate-testing/ – Jon Reid Jun 22 '18 at 00:57