25

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
  • 6,388
  • 2
  • 28
  • 34
  • 1
    Note @quelish's answer below. This is now available in Xcode 7 via contextual menu on the test itself - Thanks Xcode team! – Dad Jan 31 '16 at 04:16

5 Answers5

63

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

instruments profile unit test memory

quellish
  • 21,123
  • 4
  • 76
  • 83
  • 2
    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. – Dad Jan 31 '16 at 04:15
  • 4
    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. – fzwo Oct 17 '16 at 07:15
  • In Xcode 8 this may only work consistently with UI tests. – quellish Oct 17 '16 at 07:25
  • "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. – Alex Zavatone Jun 27 '19 at 20:16
  • 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. – Rick Apr 15 '21 at 09:01
27

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
psobko
  • 1,548
  • 1
  • 14
  • 24
  • 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? – Fergal Rooney Feb 23 '14 at 17:25
  • @FergalRooney it's going to be the whatever your application is named. Here's a [screenshot](http://i.imgur.com/XfnTM95.png) 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. – psobko Feb 24 '14 at 18:38
  • 1
    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. – Fergal Rooney Feb 25 '14 at 15:57
  • If that screenshot is any indicator, this answer seems to work for applications but not for unit test bundles. – paulmelnikow Mar 31 '14 at 01:20
  • 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. – Pellet Jul 30 '14 at 08:07
  • Nice! I answered the same question with SenTest last year what a pity i never saw this - http://stackoverflow.com/questions/10962388/running-sentestingkit-unit-tests-in-instruments/17789641#17789641 – Daniel Galasko Aug 23 '14 at 13:00
  • This may work, but requires us to profile in debug mode. – Raphael Mar 14 '17 at 10:29
  • Seemed to be the only way I could get unit test profiling to work on Xcode 13.4.1. I tried the Xcode "Profile " option, however it seemed to have issues with the app launch terminating after a couple of seconds when started via Instruments. – Bugmeister Jul 07 '22 at 01:58
10

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

Pol
  • 3,848
  • 1
  • 38
  • 55
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];
Alex Zavatone
  • 4,106
  • 36
  • 54
Pellet
  • 2,254
  • 1
  • 28
  • 20
  • 1
    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. – tmandry Dec 13 '15 at 20:31
0

Profile prompt to Instruments like Activity Monitor, Allocations, Time Profiler...

There are several notes:

Application

  • Activity Monitor is available only for real device

Unit Testing Bundle

  • Is available only for Simulator
  • Activity Monitor is not available

UI Testing Bundle

  • Profiling is not available
yoAlex5
  • 29,217
  • 8
  • 193
  • 205