Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run single test case in Xcode?

I know it is best practice to run all unit test cases after any change to make sure not breaking anything. However, some times, e.g. debugging, I really want to run only one single test case. It seems Xcode doesn't provide any such feature in UI, while other testing framework such as JUnit has such features.

Is there any workaround to have only one testcase run in Xcode?

P.S. most of my test cases are logic tests. So, they are not run in iPhone device.

like image 233
Morgan Cheng Avatar asked Aug 04 '10 03:08

Morgan Cheng


People also ask

How do I run a single test in Xcode?

With a keyboard shortcutHit ^ ⌥ ⌘ U to run the tests for the cursor's context. If you are inside a test method, like in the example below, the context will be that method. If your cursor is in a test class, but outside a method, the context will be that test calls; all its tests will run.

How do I run a test in Xcode Swift?

Run Selected Test Case You can use ⌃⌥⌘U to trigger the test case where your text cursor is currently located. If your text cursor is highlighting an XCTestCase class, then all the test cases within that class will be run.

How do I run XCTest in Xcode?

Test Lab runs unit tests and UI tests using the XCTest framework. To run your app's XCTests on Test Lab devices, build it for testing on a Generic iOS Device: From the device dropdown at the top of your Xcode workspace window, select Generic iOS Device. In the macOS menu bar, select Product > Build For > Testing.


4 Answers

Xcode 4 now have this feature. Simply create a "run scheme"  that has the test cases that you want to run.

  1. Open menu "Product|Edit Scheme..."
  2. Click on "Edit..."
  3. In the left pane, expand the "Test" section.
  4. In the right pane, expand the test bundle and uncheck the test cases you don't need to run.
like image 140
adib Avatar answered Oct 08 '22 23:10

adib


+++U

You can also use the keyboard short cut of Control-Option-Command-U

Expert taken from Apple Documentation

Product > Perform Action > Test . This dynamic menu item senses the current test method in which the editing insertion point is positioned when you’re editing a test method and allows you to run that test with a keyboard shortcut. The command’s name adapts to show the test it will run, for instance, Product > Perform Action > Test testAddition. The keyboard shortcut is Control-Option-Command-U.

like image 33
Stuart Avatar answered Oct 09 '22 00:10

Stuart


I'm sure that no one missed the release of Xcode 5 and its ability to easily run a single test case, but adding this answer just for completeness.

In Xcode 5 you can run a single test case by clicking the little play button that you can find next to the test in the test navigator or next to the test method in the editor. In both places it shows up when you hover over it.

like image 37
Erik B Avatar answered Oct 08 '22 23:10

Erik B


You can also use xctool from the commandline with the --only argument, which will only run the specified testcase(s).

like image 22
Will Avatar answered Oct 08 '22 23:10

Will