Useful Xcode shortcuts for unit testing

⋅ 2 min read ⋅ Xcode Testing Workflow

Table of Contents

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.

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

Run a single test(s)

// Where testName is a test that our cursor poiting
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 on an XCTestCase class
The cursor is outside a test case, but within the scope of 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.
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).

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

Read more article about Xcode, Testing, Workflow, or see all available topic

Enjoy the read?

If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

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

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron Buy me a coffee Tweet Share
Previous
tintColor in SwiftUI

We will talk about accentColor, a tintColor equivalent in SwiftUI.

Next
How to remove text from a navigation bar back button

There are a lot of hacky ways to modify or remove a back button title. This tip will show you how to do it properly.

← Home