19

I am writing tests for my iPhone app using OCUnit. Is there any way to debug the unit tests and have a break point to see what the heck is happening?

It's ridiculously hard to write unit tests without being able to use breakpoints.

apaderno
  • 28,547
  • 16
  • 75
  • 90
aryaxt
  • 76,198
  • 92
  • 293
  • 442

5 Answers5

12

The link posted by David Gelhar is correct for Xcode 3.

For Xcode 4, things are much simpler. Edit your current scheme and go to the "Test" action. Click the '+' at the bottom and add the test bundle that contains the tests you want to run. Now when you choose Product -> Test, it will run those tests. Any active breakpoints will be hit just like you'd expect.

BJ Homer
  • 48,806
  • 11
  • 116
  • 129
  • Awesome, I installed xcode 4, and all I had to do was Product->Test – aryaxt Apr 07 '11 at 17:35
  • +1 Thanks for your answer, a good reason to switch to XCode4. – Nick Weaver May 06 '11 at 07:22
  • 5
    I wish that worked for me. I'm using Xcode 4, the breakpoints are on, the big switch for breakpoints is on in the schema, and I have a breakpoint on in both the testing class, and the class I want to test, and it skips past like none of those things matter. The difference may be that I'm trying to do Logic tests, rather than Application tests. – Hack Saw Sep 01 '11 at 22:11
  • It should work for both Logic tests and Application tests; it sounds like something else is wrong with your setup. Sorry! – BJ Homer Sep 02 '11 at 03:51
  • I have same issue as hack-saw. @BJHomer, "should work" is rather soft. Does that mean breakpoints work for you in logic tests? Or that you theorize they should work, but haven't verified? –  Feb 22 '12 at 21:37
  • @toolbear Breakpoints work for me in both logic tests and application tests. I use them daily at work. – BJ Homer Feb 23 '12 at 03:01
  • I had the same issue. @Daniel Sproul's answer below did the business. – Max MacLeod Sep 10 '12 at 09:18
12

Using XCode 4.2, (with SenTestKit unit tests as set up by checking the "Include Unit Tests" checkbox when setting up the project), Product->Test wasn't hitting my breakpoints, but Product->Perform Action->Test Without Building seems to do the trick. Hope this helps.

Daniel Sproul
  • 121
  • 1
  • 2
  • This didn't start working for me until I clicked the "Use the Run action's arguments and environment variables" checkbox in the "Arguments" tab of the "Test" section of the scheme that builds my project. – Jim Hayes Jul 02 '12 at 04:33
  • 1
    Make sure that you have "Test After Build" turned off in the Build Settings for your unit test target. – mamills Jun 13 '13 at 18:11
3

You may have also accidentally disabled "Debugger: Debug Executable" option in Scheme -> Test -> Info

enter image description here

1

Here's a blog post: Debugging Cocoa application unit tests with instructions for how to do this (for XCode 3 at least; not sure about XCode 4).

David Gelhar
  • 27,873
  • 3
  • 67
  • 84
0

Another item to watch for in XCode 4 is that you haven't added the classes being tested to the Unit Test Target as well as the main project. It appears that it's not necessary and it will mess up your ability to have breakpoints hit.

A clue will be warning messages in the debug log when you run. The messages will look like this:

"Class XXX is implemented in both YYY and ZZZ. One of the two will be used. Which one is undefined."

Once I removed the classes noted in the warnings from the unit test target, Xcode started hitting the breakpoints.

You can remove classes from a target by clicking on the .M file, and turning off its membership in the unit test target in the inspector window under "Target Membership".

Matt__C
  • 319
  • 2
  • 10
  • I also noted some bizarre behaviour with member variables not staying set, etc. which went away when I fixed the targets. – Matt__C May 24 '11 at 17:57