Is it possible to run XCTest based tests under Instruments in Xcode 5?

12,369

Solution 1

I think this is the easiest way:

  1. Set a breakpoint somewhere in your tests (I've been doing it in the setup method)
  2. Open a new document in instruments
  3. Run the application and make sure it's stopped at a breakpoint
  4. From the Target drop down in Instruments choose Attach to Process and scroll down to your process
  5. Click on record and then resume in XCode

Solution 2

In Xcode, right click on your test in the test navigator and select "Profile "TestName"":

instruments profile unit test memory

Solution 3

Here's the right way to do it under Xcode 6:

1) In your Xcode project, reveal the "Products" folder, select the ".xctest" product, right-click and finally choose "Reveal in Finder" in the contextual menu.

2) Launch Instruments and create a new document with the template of your choice.

3) Do "Choose Target..." for the document

4) Navigate and select the tool Xcode uses to run tests located at /Applications/Xcode.app/Contents/Developer/usr/bin/xctest (you can find this location using xcrun -f xctest in Terminal) - you will need to enable "Traverse Packages" to navigate inside the Xcode app.

5) Drag & drop from the Finder into the "Arguments" field the ".xctest" product revealed at step 1 - this enters its absolute path.

6) Click "Choose".

You are now ready to run your unit tests from Instruments!

enter image description here

Solution 4

I created a new target based on the mac app target I am testing, then added the xctest classes to the files to compile.

I then added the /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework to the frameworks to link against.

Then added its path $(DEVELOPER_DIR)/Platforms/MacOSX.platform/Developer/Library/Frameworks to the Runpath Search Paths.

Then in the main.m I added the following:

    XCTestSuite *suite = [XCTestSuite testSuiteWithName:@"My tests"];
    [suite addTest:[VideohogCachingTest testCaseWithSelector:@selector(testCompleteSequentialText)]];
    [suite run];

This ran the test testCompleteSequentialText on the class VideohogCachingTest I needed to run as a normal application, therefore allowing me to run the test by either command+R or in this case, profiling with command+I. I could then catch a zombie and trace the problem, which was not a possibility for me previously. If you'd like to run all your tests you can do:

XCTestSuite *suite = [XCTestSuite defaultTestSuite];
[suite run];
Share:
12,369

Related videos on Youtube

Dad
Author by

Dad

Updated on November 15, 2020

Comments

  • Dad
    Dad over 3 years

    Looking at XCTest and Xcode 5 unit testing and not seeing how to run my unit tests under Instruments in Xcode 5...

    Specifically I'd like to look for leaks during the test (this doesn't have to be automated in this instance, though clearly that'd be useful).

    Possible?

    • Dad
      Dad over 8 years
      Note @quelish's answer below. This is now available in Xcode 7 via contextual menu on the test itself - Thanks Xcode team!
  • Fergal Rooney
    Fergal Rooney over 10 years
    I'm looking at the same problem. I have zombie in my unit tests and I can't figure out whats causing it. What process should I select?
  • psobko
    psobko over 10 years
    @FergalRooney it's going to be the whatever your application is named. Here's a screenshot of attaching it to a test project I created named ShapeAnimationTest. I forgot to mention in my original answer that you need to run the application before finding it in Instruments.
  • Fergal Rooney
    Fergal Rooney over 10 years
    Thanks for the screenshot. I tried it by selecting my application like in the steps you listed, but I do not see any allocations listed when I run the instrument and after I continue the test after setup breakpoint. Seems like its not profiling the app at all.
  • paulmelnikow
    paulmelnikow about 10 years
    If that screenshot is any indicator, this answer seems to work for applications but not for unit test bundles.
  • Pellet
    Pellet almost 10 years
    This works for test bundles for me by choosing the 'xctest' process. However if I need to trace zombies, I needed to do this differently, see my answer for another method.
  • Daniel Galasko
    Daniel Galasko almost 10 years
    Nice! I answered the same question with SenTest last year what a pity i never saw this - stackoverflow.com/questions/10962388/…
  • tmandry
    tmandry over 8 years
    This helped me because it seems you have to compile for profiling to get Swift symbols in Allocations. Note that I had to add that path also to the Framework Search Paths, and edit the scheme to make Profile use the Debug configuration.
  • Dad
    Dad over 8 years
    Yes! Feature request worked! Now available in Xcode. (wasn't in Xcode 5). Thanks for pointing it out to folks who might find this old question.
  • fzwo
    fzwo over 7 years
    This seems to be broken with Xcode 8. The menu item is still there, but Instruments just shows "all processes", and Xcode doesn't start testing.
  • quellish
    quellish over 7 years
    In Xcode 8 this may only work consistently with UI tests.
  • Raphael
    Raphael over 7 years
    This may work, but requires us to profile in debug mode.
  • Alex Zavatone
    Alex Zavatone about 5 years
    "Failed to install the requested application". "An application could not be found at the provided path." Xcode 10.2.1. The solution was that I needed to add our app to the Profile section of our testing scheme. Still, it's not 100% yet.
  • Rick
    Rick about 3 years
    This launches instruments for me, but doesn't seem to go any further. No obvious errors. It prompts me to choose a template, opens a new doc, and then…nothing. Xcode 12.4 on Bug Sur.