44

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.

6 Answers 6

50

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.
4
  • I typically use 2 schemes, one for running all tests that I run daily, and another scheme for running specific tests I'm concerned with at the moment. In this way I can keep the slow running tests in the daily scheme and they don't slow down current development. Mar 6, 2013 at 20:31
  • Can editing the scheme for currently selected test is doable via an apple script ? via a simple right click -> run this test
    – ıɾuǝʞ
    Mar 9, 2013 at 6:58
  • 3
    Is there a way I can downvote apple for this? With a real-world number of test-cases this is useless.
    – Simon
    Jul 31, 2013 at 13:26
  • For Xcode 7, click the diamond next to the test case class name in the editor gutter.
    – adib
    Mar 3, 2016 at 2:58
38

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

2
  • 1
    Beautiful. This method is far more convenient than the accepted answer.
    – Snowman
    Oct 8, 2015 at 14:46
  • Doesn't work in Xcode 7 (at least not when Xcode is loading the test index). Aug 24, 2016 at 15:53
13

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.

3

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

0

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.