You can quickly clean, build and test your project/workspace in the Terminal with the xcodebuild tool. The following shows the basic structure of the command:

xcodebuild [clean, build, test]
[-project/-workspace <your_project/workspace_name>]
-scheme <your_scheme_name>
-destination <destination-specifier>

Example for Running All Tests

The following command will run all the tests of the MyProject workspace, running MyScheme, for the Debug configuration on the iPad Air simulator. Please notice the \ at the end of each line. This allows you to run a multi-line command on your shell, which can improve readability and make spotting mistakes easier.

$ xcodebuild test \
-destination 'platform=iOS Simulator,name=iPad Air' \
-sdk iphonesimulator \
-workspace MyProject.xcworkspace \
-scheme MyScheme \
-configuration Debug

You can also pass different options to test only a test case, a single test or even skip test cases/tests. For a detailed overview, including more command examples you can look at Apple's developer site, or for a quick reference to the command and its options you can check the manual pages with:

$ man xcodebuild

Formatting with xcpretty

To make the format more readable and get colors that can help you identify slow tests, you can pipe xcodebuild’s output in xcpretty, a fast and flexible formatter for xcodebuild.

$ xcodebuild test \
-destination 'platform=iOS Simulator,name=iPad Air' \
-sdk iphonesimulator \
-workspace MyProject.xcworkspace \
-scheme MyScheme \
-configuration Debug | xcpretty

The gem provides more options for customizing the output and even creating a report in XML, HTML and JSON formats to examine later.

You can find all about xcpretty on its GitHub page.

Establishing a Workflow

Although it is suggested you store the xcodebuild command for future reference, it can be inconvenient to copy and paste into the Terminal every time you wish to build or run your test suites. Instead, you can create shortcuts in your shell profile that can help expedite the process and execute any commands you want with only a few keystrokes.

If you're using the bash shell then in your ~/.bash_profile you can create an alias as such:

alias testmyproject="xcodebuild test \
-destination 'platform=iOS Simulator,name=iPad Air' \
-sdk iphonesimulator \
-workspace MyProject.xcworkspace \
-scheme MyScheme \
-configuration Debug | xcpretty"

Then on the command line, you can run xcodebuild by executing:

$ testmyproject

In case you're using zsh, you can paste the same alias in your ~/.zshrc.

Please remember to restart the terminal for the alias to be available for use.