# iOS Unit Testing and UI Testing Tutorial

**URL:** <https://forums.kodeco.com/t/ios-unit-testing-and-ui-testing-tutorial/27170>\
**Category:** 🎓 Courses & Articles\
**Created:** [March 13, 2017, 6:46pm UTC](https://forums.kodeco.com/t/ios-unit-testing-and-ui-testing-tutorial/27170 "2017-03-13T18:46:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [March 13, 2017, 6:46pm UTC](https://forums.kodeco.com/t/ios-unit-testing-and-ui-testing-tutorial/27170/1 "2017-03-13T18:46:54Z")

</div>

Learn how to add unit tests and UI tests to your iOS apps, and how you can check on your code coverage.

* * *
 This is a companion discussion topic for the original entry at [https://www.raywenderlich.com/709-ios-unit-testing-and-ui-testing-tutorial](https://www.raywenderlich.com/709-ios-unit-testing-and-ui-testing-tutorial)

---

<div class="post-metadata">

**Author:** ![jrg.developer](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jrg.developer/32/109958_2.png) [@jrg.developer](https://forums.kodeco.com/u/jrg.developer)\
**Post date:** [March 13, 2017, 7:41pm UTC](https://forums.kodeco.com/t/ios-unit-testing-and-ui-testing-tutorial/27170/2 "2017-03-13T19:41:35Z")

</div>

> Writing tests isn’t glamorous

It isn’t!? I love writing tests. 😉

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [March 13, 2017, 11:42pm UTC](https://forums.kodeco.com/t/ios-unit-testing-and-ui-testing-tutorial/27170/3 "2017-03-13T23:42:24Z")

</div>

I’ll bet you’re in high demand! and yes, I really enjoyed writing this article :]

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [March 13, 2017, 11:55pm UTC](https://forums.kodeco.com/t/ios-unit-testing-and-ui-testing-tutorial/27170/4 "2017-03-13T23:55:13Z")

</div>

**Addendum:** Often, you need to make some changes to the app code in order to write tests. I didn’t do this in the article — I think I pre-tweaked my apps so it wouldn’t be necessary — but I think Xcode is a bit more stable if you do the tweaks _before_ you import the app into the test bundle. Otherwise, you might have to restart Xcode/your Mac multiple times, before it stops saying it can’t find your app!

---

<div class="post-metadata">

**Author:** ![mrmuetze](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mrmuetze/32/60131_2.png) [@mrmuetze](https://forums.kodeco.com/u/mrmuetze)\
**Post date:** [March 14, 2017, 11:42am UTC](https://forums.kodeco.com/t/ios-unit-testing-and-ui-testing-tutorial/27170/5 "2017-03-14T11:42:56Z")

</div>

Hey Audrey,  
awesome guide, thank you very much!

While looking into how to apply this to my app, a couple of questions came to my mind. One of them is the following:

I wanted to start with adding tests to the requests I send with AFNetworking to our backend. The natural start would be testing authentication for login, logout and the such. The issue is that I save the token in a singleton after authentication is successful and don’t necessarily return it as part of the login method. Since testing async network calls, testing the logout call before login has finished is not a given (both login and logout work with the singleton that holds the user authentication status).

Is there a general best practice for testing user authentication? Should I update the input and output params of my methods?

Thanks again! :] This’ll get me started into the world of testing.

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [March 14, 2017, 1:29pm UTC](https://forums.kodeco.com/t/ios-unit-testing-and-ui-testing-tutorial/27170/6 "2017-03-14T13:29:22Z")

</div>

hi Björn: I’m not a testing expert, but the testing wizards I know seem to exert a lot of effort refactoring code to make it easier to test, and adding parameters to methods ranks high on the list: to expose hidden dependencies, so making it easier to inject dependencies. The Michael Feathers book I link to at the end of the tutorial is a valuable resource for learning new ways to think about designing code to be testable.

Is your authentication OpenID Connect/OAuth2? I adapted some Stormpath code for my upcoming URLSession video course, and stored the tokens in keychain, using [KeychainWrapper](https://github.com/jrendel/SwiftKeychainWrapper).

I’m not sure what you mean by “not a given”: if you need a way to ensure login has finished before you try to logout, you might try wrapping them as asynchronous Operations, and setting a dependency. This is covered in our Concurrency video course.

---

<div class="post-metadata">

**Author:** ![joekim](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/joekim/32/87881_2.png) [@joekim](https://forums.kodeco.com/u/joekim)\
**Post date:** [March 14, 2017, 7:44pm UTC](https://forums.kodeco.com/t/ios-unit-testing-and-ui-testing-tutorial/27170/7 "2017-03-14T19:44:15Z")

</div>

@audrey Great article! Testing is rarer than it should be for mobile development. I think the biggest reason is the massive view controller problem. Hopefully, movement towards function programming, value-oriented programing and unidirectional data flow will help as it makes code more compose-able and testable.

[https://www.raywenderlich.com/114456/introduction-functional-programming-swift](https://www.raywenderlich.com/114456/introduction-functional-programming-swift)

> **[Let's Play: Refactor the Mega Controller!](https://academy.realm.io/posts/andy-matuschak-refactor-mega-controller/)**
>
> So you’ve got a huge view controller that knows about everything. It’s become a puppeteer mastermind whose responsibilities have somehow g…

> **[GitHub - ReSwift/ReSwift: Unidirectional Data Flow in Swift - Inspired by Redux](https://github.com/ReSwift/ReSwift)**
>
> Unidirectional Data Flow in Swift - Inspired by Redux - GitHub - ReSwift/ReSwift: Unidirectional Data Flow in Swift - Inspired by Redux

In terms of next steps, how about a tutorial using buddy build as a CI with automated testing? They have a free tier which could be helpful for those focused on learning.

- As a side note, I used to work at SVL, too. I was there from 2003-2006.

---

<div class="post-metadata">

**Author:** ![cgchinmay](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/cgchinmay/32/60199_2.png) [@cgchinmay](https://forums.kodeco.com/u/cgchinmay)\
**Post date:** [March 14, 2017, 11:34pm UTC](https://forums.kodeco.com/t/ios-unit-testing-and-ui-testing-tutorial/27170/8 "2017-03-14T23:34:13Z")

</div>

Nice explanation but I still find UI testing very cumbersome.

UI testing looks easy when demonstrated with such simple examples like testing for existence of UI elements. But it becomes very difficult when you have to check for the contents of these elements .  
Let’s say I want to check whether correct image is displayed in the image view.  
I tried using the query selector in xcode but it’s confusing . Sometimes you just cannot access the image view or it’s properties.  
So how can we achieve something like this ? Is there any simple way to know what kind of queries we can use on UI elements ?

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [March 15, 2017, 12:03am UTC](https://forums.kodeco.com/t/ios-unit-testing-and-ui-testing-tutorial/27170/9 "2017-03-15T00:03:40Z")

</div>

yes, there are many blog posts expressing disappointment with UI Testing; hopefully Apple is working to improve it, and their documentation.

[https://blog.metova.com/guide-xcode-ui-test/](https://blog.metova.com/guide-xcode-ui-test/) looks like a good resource. It summarises a lot from Joe Masilotti’s articles/github, much of which has been taken down, at Apple’s request. But the metova article links to their own forked copy.

Masilotti’s [UI Testing cheat sheet and examples | Masilotti.com](http://masilotti.com/ui-testing-cheat-sheet/) is still up, and looks really useful, although the last reply from Masilotti was 10 months ago.

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [March 15, 2017, 12:10am UTC](https://forums.kodeco.com/t/ios-unit-testing-and-ui-testing-tutorial/27170/10 "2017-03-15T00:10:48Z")

</div>

Agree about the MVC problem!

And CI/CD is on the wishlist, but it would be about Xcode Server. I know Ray accepts commissions from companies, like his current screencasts on Perfect, so if you know anyone at buddy build …

---

<div class="post-metadata">

**Author:** ![joekim](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/joekim/32/87881_2.png) [@joekim](https://forums.kodeco.com/u/joekim)\
**Post date:** [March 15, 2017, 1:11am UTC](https://forums.kodeco.com/t/ios-unit-testing-and-ui-testing-tutorial/27170/12 "2017-03-15T01:11:42Z")

</div>

@audrey I know some people at buddy build. Can you DM me about commissions?

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [March 15, 2017, 1:37am UTC](https://forums.kodeco.com/t/ios-unit-testing-and-ui-testing-tutorial/27170/14 "2017-03-15T01:37:03Z")

</div>

wait, that wasn’t a reply to you, Joe

paging @raywenderlich

or contact Ray directly?

---

<div class="post-metadata">

**Author:** ![joyseedog](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@joyseedog](https://forums.kodeco.com/u/joyseedog)\
**Post date:** [March 17, 2017, 9:15am UTC](https://forums.kodeco.com/t/ios-unit-testing-and-ui-testing-tutorial/27170/15 "2017-03-17T09:15:10Z")

</div>

Thanks @audrey ,very helpful !

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [March 24, 2017, 6:14am UTC](https://forums.kodeco.com/t/ios-unit-testing-and-ui-testing-tutorial/27170/16 "2017-03-24T06:14:49Z")

</div>

you’re very welcome!

---

<div class="post-metadata">

**Author:** ![kmt](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/kmt/32/62864_2.png) [@kmt](https://forums.kodeco.com/u/kmt)\
**Post date:** [April 9, 2017, 5:25pm UTC](https://forums.kodeco.com/t/ios-unit-testing-and-ui-testing-tutorial/27170/17 "2017-04-09T17:25:52Z")

</div>

Hi Audrey,

Thanks very much for the article. It clears a lot of things up on a project I just inherited. However, I am stuck on one spot in this tutorial. You say:

_Open the little menu next to [“Slide”] and select segmentedControls.buttons[“Slide”]_

I am using Xcode 8.2.1 and cannot find this “little menu” anywhere. Could you take a quick screen shot of where this is so I can continue?

Thanks  
kt

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [April 9, 2017, 11:45pm UTC](https://forums.kodeco.com/t/ios-unit-testing-and-ui-testing-tutorial/27170/18 "2017-04-09T23:45:39Z")

</div>

hi kt: it appears when you record:

 ![](https://raywenderlich-com-v3-forums.s3.amazonaws.com/original/3X/5/1/5186fdf8296ed033f5943f6520d912ba37fe28d2.png)

Stop recording; it should still be there. It can be a little tricky getting it to open its menu:

 ![](https://raywenderlich-com-v3-forums.s3.amazonaws.com/original/3X/1/1/11ed1b268bd132528f457fde4737ac800cbc64e0.png)

btw Joshua Greene has updated the [Testing video course](https://videos.raywenderlich.com/courses/beginning-ios-unit-and-ui-testing/lessons/1), and goes into more detail about unit and UI testing.

---

<div class="post-metadata">

**Author:** ![kmt](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/kmt/32/62864_2.png) [@kmt](https://forums.kodeco.com/u/kmt)\
**Post date:** [April 10, 2017, 4:10am UTC](https://forums.kodeco.com/t/ios-unit-testing-and-ui-testing-tutorial/27170/19 "2017-04-10T04:10:07Z")

</div>

Hi A,

Thanks so much for responding, on a Sunday no less. I got it, your explanation was great. One of the problems was that I was tapping on the “slide” itself, at the top, not the Slide/Type switch at the bottom right. This was giving me a different outcome and hence, no menu to choose. Once I actually tapped on the right thing, the menu came up like you said.

Much appreciated.  
kt

---

<div class="post-metadata">

**Author:** ![pavoleg](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/pavoleg/32/111579_2.png) [@pavoleg](https://forums.kodeco.com/u/pavoleg)\
**Post date:** [April 13, 2017, 3:21pm UTC](https://forums.kodeco.com/t/ios-unit-testing-and-ui-testing-tutorial/27170/20 "2017-04-13T15:21:59Z")

</div>

Hi, Audrey!  
Great post, thank you!  
Why do you use:

```
controllerUnderTest = UIStoryboard(name: "Main", 
      bundle: nil).instantiateInitialViewController() as! SearchViewController!

```

to create instance instead of just:  
`controllerUnderTest = SearchViewController()`

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [April 13, 2017, 10:24pm UTC](https://forums.kodeco.com/t/ios-unit-testing-and-ui-testing-tutorial/27170/21 "2017-04-13T22:24:53Z")

</div>

This instantiates the view controller from the storyboard, ensuring that all its outlets are also instantiated. I think it’s not really necessary in this particular case (testing that response is parsed correctly), but it’s a good habit to get into, in case your test checks the state of the view controller’s outlets.

In this case, too, the SUT is the view controller only because the networking code is embedded in the view controller, instead of separated into its own module.

---

<div class="post-metadata">

**Author:** ![pavoleg](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/pavoleg/32/111579_2.png) [@pavoleg](https://forums.kodeco.com/u/pavoleg)\
**Post date:** [April 14, 2017, 1:44pm UTC](https://forums.kodeco.com/t/ios-unit-testing-and-ui-testing-tutorial/27170/22 "2017-04-14T13:44:42Z")

</div>

Thank you for explanation!

[Next page](https://forums.kodeco.com/t/ios-unit-testing-and-ui-testing-tutorial/27170.md?page=2)
