Useful Xcode shortcuts for unit testing

Testing is a process we do along with our development. Knowing shortcuts would help you save some time, which will add up in the long run.

Run all tests #

Menu Product > Test

or ⌘ - command + U

This command will run all tests in the current scheme.

Use case #

Use it when you want to run all of your tests. This is the easiest to remember, and I use this all the time.

Run a single test(s) #



Menu Product > Perform Action > Run < testName >

or ⌃ – control + ⌥ – option + ⌘ - command + U

This command runs a test case where your cursor is currently located.

If your cursor is on an XCTestCase class name or outside a test case, but within the scope of XCTestCase class, then all the test cases within that class will be run.

The cursor is on an XCTestCase class

The cursor is outside a test case, but within the scope of XCTestCase class

This shortcut is the same as running the test by click on the indicator next to each test method name.

Hold the pointer over the line number of the test name and click to run the test.

Use case #

I normally use this when I add a new test case. At the time, I finish writing my case, my cursor usually within a scope of the case. No need to move my cursor to that little indicator.

Rerun Previous Test(s) #

Menu Product > Perform Action > Run < testName > Again

or ⌃ – control + ⌥ – option + ⌘ - command + G

This is a very handy command. It reruns the last test method executed.

Use case #

I use this when debugging my failed test case. You can edit your code and use this shortcut to run the last test without going back to your test case.

Build for test #

Menu Product > Build for > Testing

or ⇧ - SHIFT + ⌘ - command + U

This command will build the test bundle products without running the tests.

Use case #

When develop a new feature. Building ⌘ - command + B and Runing ⌘ - command + R won't build your test bundle, so at the time you finish your feature, a lot of your tests might not even compile.

This command is useful when changing code to check for warnings and compile errors of your tests. The sooner you know the error, the easier to fix. I try to run this along with build command (but I always forgot the shortcut).

Related Resources #

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

← Home