Running unit tests from the command line with xcodebuild

Previously, we discussed how Xcode 9.4.1 cannot run unit tests for Frameworks on macOS Mojave. I mentioned there that the workaround is to run unit tests from the command line, and a reader wrote in to ask how to do that. Here’s the xcodebuild command for one of my projects:

xcodebuild clean test -project Repos.xcodeproj -scheme "Repos" -destination "name=iPhone X,OS=12.1"

I always like to run clean first since I’ve seen so many issues that are easily resolved with a clean. The test subcommand tells it to run your unit tests in the given scheme. And finally, the destination here tells it to run in the iPhone X simulator running iOS 12.1.

If you’re using an xcworkspace rather than an xcodeproj, you can replace the -project part with -workspace and give it the name of your workspace instead. Happy testing!