DEV Community

higuri
higuri

Posted on

How to Run Individual XCTestCases

Background

You can run XCTest (unit tests for your Xcode project) easily with 'Run Test' ('Product'>'Test' or Command+U) on Xcode. This is useful if you make extensive changes to your product code and want to test them all at once because 'Run Test' executes all the test cases implemented in your project.

But there are times when you want to run individual test cases, such as while fixing bugs for a specific method and want to run only test cases for the method.

Xcode

Edit Scheme

Select 'Product' > 'Scheme' > 'Edit Scheme' > 'Test' pane > 'Info' tab, and then check and uncheck test case names. By doing this, you can specify which test cases are executed with 'Run Test'. This method is more troublesome as you implement more test cases...
Screenshot of Edit Scheme

Test Navigator

Show 'Test Navigator' in the left side panel of the Xcode window, and push the 'PLAY' icon displayed to the right of the test case name.
Screenshot of Test Navigator

Run Current Test

Control + Option + Command + U
This executes a test case in which your cursor is.

Re-Run Last Test

Control + Option + Command + G
This re-runs the last test cases executed.

xcodebuild

You may find value in specifying test cases with command line interface, if you have many test cases and want to switch execution targets frequently. xcodebuild is one of the command line tools bundled with Xcode.

Sample Usage

> xcodebuild test \
>   -workspace MyXCWorkSpace \
>   -scheme MyAppScheme \
>   -destination "platform=iOS,name=my_iphonex" \
>   -only-testing:MyXCTargetForTest/MyTestSuite/MyTestMethod
Enter fullscreen mode Exit fullscreen mode

Remarks

Xcode Version: 9.2

References

Is there a keyboard shortcut in Xcode 6 to run a single current test function under cursor?
https://stackoverflow.com/q/28057643

Running individual XCTest (UI, Unit) test cases for iOS apps from the command line
https://stackoverflow.com/q/35166214

How to use xcodebuild with -only-testing and -skip-testing flag?
https://stackoverflow.com/q/39427263

Top comments (1)

Collapse
 
danskeel profile image
DanSkeel

If you disable all unwanted tests you can run single test multiple times without building with command Perform action > test without building (control + cmd + U)