Testing for watchOS Apps (Apple Watch)

Testing for watchOS Apps (Apple Watch)

Update (2.18.21): With the upcoming update to Xcode (12.5), Xcode will support XCTest unit and UI tests for watchOS! Even though, this post includes some helpful tips on structuring your code/tests and enabling tested code sharing for other targets, including extension that still don’t support tests out of the box.

Summary

When Apple Watch only apps were announced, I was excited and immediately jumped to create a standalone Apple Watch app! Even though I enjoy working on my watch apps, the development and developer experience lacks — almost as if support for independent watch apps was a rushed thought. Here are just some of the things that stood out to me (some of which I filed a radar for):

Addressing Lack of automated testing support is the subject of this post. I find a lot of value in automated testing, so I was real bummed that Apple allowed independent Apple Watch apps without automated testing support (XCTest). In hindsight, I probably should have expected this since testing doesn’t seem to be a major focus from the Apple dev tools side. After some soul searching and digging, I found a way to add tests to independent watchOS apps so I figured I would share!


Automated Testing for watchOS Apps

Note: If Xcode stops working as expected (like not being able to build or errors aren’t showing up correctly), restart Xcode. I have had to do this more times than I care to admit 😅.

Hello Swift Package Manager

The first thing to point out is that this is a work around for the lack of testing support for watchOS apps. As mentioned above, there is no out of the box support for testing watchOS code. So we need another place to put our code that both allows testing and can be imported into the watchOS app. This is where Swift Package Manager (SPM) comes in! The high level idea is to add a local swift package using SPM that includes targets for both watchOS and another platform that supports testing like iOS.

Initial setup

Testing setup

At this point, we have a local swift package integrated and available for use in the watchOS app 🎉. That is great, but we haven’t actually tested anything. We will, but first I should note some things about the swift package for clarity (this isn’t a post on SPM, so this list doesn’t aim to be comprehensive).

Now that we know a little more about the way things fit together for a swift package, let’s do some testing!

Running tests from the watchOS apps scheme

Lets take this one step further and add the ability to run these tests from the watchOS app scheme for ease of running. This isn’t hard to do, and also has the side benefit of working well in a CI/CD system.


Additional thoughts


Feel free to reach out on Twitter — cheers!

rss facebook twitter github youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora