Morgan Cheng
Morgan Cheng

Reputation: 75998

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.

Upvotes: 46

Views: 22259

Answers (7)

raddevus
raddevus

Reputation: 9077

It's even easier now in Xcode 14.x (14.3.1 in my case).

  1. Click the checkmark icon in the Project Navigator (highlighted in red box in the image below)
  2. This will take you to your list of tests.
  3. Right-click on the test you want to run and select the option from the context menu (shown in image)

Xcode project manager - unit tests

Upvotes: 0

Sergey
Sergey

Reputation: 79

  • Go to View -> Navigators -> Tests (⌘-6)
  • Find your test case or test and secondary click on it
  • Run your case/test

Upvotes: 0

adib
adib

Reputation: 8395

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.

Upvotes: 51

Will
Will

Reputation: 24689

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

Upvotes: 3

Stuart
Stuart

Reputation: 659

+++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.

Upvotes: 38

Erik B
Erik B

Reputation: 42554

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.

Upvotes: 14

Philippe
Philippe

Reputation: 925

As you have noted, the OCUnit test framework marks methods whose name start with 'test' as test cases. This is done at runtime

In practice, your test cases should run so fast that it should not matter how many are enabled; your debugger should be able to stop inside your test case very quickly after you press "Debug".

That being said, the quickest way to disable some tests is probably to use an #if 0 / #endif block. The feature to disable test cases dynamically does not exist in Xcode / OCUnit, since there is no GUI component.

In theory it should be doable, because at runtime (and before all tests are run) there are ways to access the test list in OCUnit, but this requires modifications to the OCUnit source code, which is not desirable (it will be wiped out in the next Xcode update, for one).

Finally, if that feature is important to you, you can easily write your own test harness that mostly replicates what OCUnit does. Then you can tweak it to your heart's content, add UI, etc.

It is not difficult, and a little educational. Here's a good link to get you started:

http://gusmueller.com/blog/archives/2009/10/how_to_write_your_own_automated_testing_framework.html

Upvotes: 0

Related Questions