3

I'm trying to set up my unit testing for an iPhone application using XCode 3.2.3.

I followed the steps in http://developer.apple.com/iphone/library/documentation/xcode/conceptual/iphone_development/135-Unit_Testing_Applications/unit_testing_applications.html

As closely as I could, but I am getting this error message upon trying to build: "((yourApplicationDelegate) != nil)" should be true. UIApplication failed to find the AppDelegate

(this being the default trivial test for USE APPLICATION UNIT TEST is true)

I am building my app test target, as is discussed in the documentation, and get this error.

Do I need to ensure all my non-test case source is in my test bundle?

confused

--

I included the source files into my project and it compiles fine, but it still suffers the same unit test error:

"((yourApplicationDelegate) != nil)" should be true. UIApplication failed to find the AppDelegate

The original code is the same as the template code:

-(void) testAppDelegate {

    id yourApplicationDelegate = [[UIApplication sharedApplication] delegate];
    STAssertNotNil(yourApplicationDelegate, @"UIApplication failed to find the AppDelegate");
}
groundhog
  • 4,770
  • 2
  • 24
  • 24
  • 1
    Yes, your application source should be included in the Compile Sources build phase of your test target. Can you post your unit test code and the actual error? – Christopher Pickslay Jul 08 '10 at 19:28
  • @chrispix - side question, is there a way to automatically sweep all code into the test target without having to explicitly add them each time to create a new class? – groundhog Jul 09 '10 at 02:28
  • 1
    I don't know of any way to "automatically" sweep it into the test target. However, when you use XCode to create a new class, the dialog asks you which targets to add it to, and defaults to whatever targets you selected the previous time. In my experience, every class I add gets added to my test target--it's either a test class or a class in my app that will be tested in some way. So my test target is always checked in that dialog. The problem is actually the opposite--remembering not to include test classes in your application target. – Christopher Pickslay Jul 09 '10 at 05:39
  • 1
    For now, I'd like to add a heartfelt: Me too. I found a code example at http://developer.apple.com/IPhone/library/samplecode/iPhoneUnitTests/Introduction/Intro.html and it suffers from the exact same problem. I've filed a documentation bug, Bug ID# 8204383. – Elise van Looij Jul 18 '10 at 15:38
  • I'm having this problem too. Did you ever work it out? – Michael Forrest Oct 28 '10 at 15:23

5 Answers5

10

Make sure that BUNDLE_LOADER and TEST_HOST build settings for the unit test bundle are specified.

BUNDLE_LOADER = $(BUILT_PRODUCTS_DIR)/YourApp.app/YourApp
TEST_HOST = $(BUNDLE_LOADER)

Furthermore, In Xcode4, many of steps described in the Apple's guide are not needed (e.g. duplicating app target). I recommend that creating new project including unit test and look around.

hiroshi
  • 6,871
  • 3
  • 46
  • 59
1

This is for the OP, in case the problem persisted and is still relevant, and for the forum browsers:

I, too, followed the gudielines in the iOS Development guide on Unit and Application testing. I, too, when following the Application testing guide, got a problem as per:

Test Suite 'FirstSuite' started at 2010-12-08 21:30:33 GMT 0000
Test Case '-[FirstSuite testAppDelegate]' started.
/Users/TomCruise/Documents/iPhoneDevelopment/PickerLeak/FirstSuite.m:22: error:-[FirstSuite testAppDelegate] : "((app_delegate) != nil)" should be true. Cannot find the application delegate.
Test Case '-[FirstSuite testAppDelegate]' failed (0.000 seconds).

I.e., the application delegate was not found as expected.

THE PROBLEM WAS that when I thought I followed the guidelines, I did not. Apparently the Application testing runs only on Device. So I switched from Simulator to Device (for AppTesting target), connected my iPhone, and then did Build + Run (cmd+return/enter). The application installed itself, and a few seconds later exited with return code 1 (which I guess signifies a faulty return code).

After the app had quit, I did shift + cmd + R, (uparrow+cmd+R) to get the log prompt, and could read the following:

2010-12-08 22:59:59.163 PickerLeak[5356:307] -[UIPickerView setFrame:]: invalid height value 200.0 pinned to 180.0 
Test Suite 'All tests' started at 2010-12-08 22:00:00 GMT 0000
Test Suite '/var/mobile/Applications/{number}/PickerLeak.app/PickerLeakTests.octest(Tests)' started at 2010-12-08 22:00:00 GMT 0000
Test Suite 'FirstSuite' started at 2010-12-08 22:00:00 GMT 0000
**Test Case '-[FirstSuite testAppDelegate]' started.
Test Case '-[FirstSuite testAppDelegate]' passed (0.001 seconds).**
Test Case '-[FirstSuite testFail]' started.
/Users/TomCruise/Documents/iPhoneDevelopment/PickerLeak/FirstSuite.m:17: error: -[FirstSuite testFail] : Must... fail.
Test Case '-[FirstSuite testFail]' failed (0.003 seconds).
Test Suite 'FirstSuite' finished at 2010-12-08 22:00:00 GMT 0000.
Executed 2 tests, with 1 failure (0 unexpected) in 0.004 (0.008) seconds

So... Hooray! :)

Godspeed, developers.

stackdaemon
  • 309
  • 4
  • 10
1

I've falled twice on this. You need to setup BUNDLE_LOADER and TEST_HOST build settings to make it work. But it is not enough, you need to also set Bundle Loader and Test Host. Please check how to implement application tests in xcode4?, that answer worked perfectly fine for me.

Community
  • 1
  • 1
Sebastian Juarez
  • 3,317
  • 2
  • 21
  • 20
0

I can't directly answer your question, because I also never got application tests to work with OCUnit. I understand a number of people use the Google Toolbox for Mac iPhone unit testing library or GHUnit to make this easier.

As the Blazing Cloud post linked above points out, OCUnit (and GTM/GHUnit) are more JUnit-style than Rspec-style. At Pivotal we use Rspec for nearly everything, and we wanted something similar for Objective C. So, we wrote Cedar, and we intentionally designed it to work with UIKit, run on iOS devices, etc. It's freely available and open source; if you have any questions about it you can email the discussion group: cedar-discuss@googlegroups.com.

Adam Milligan
  • 2,826
  • 19
  • 17
0

According to Apple's documentation, there are 2 kind of unit tests.

  • logic tests
  • application tests

The application tests are working only on hardware device. If you're using simulator, it won't work.

See this: http://developer.apple.com/library/ios/#documentation/Xcode/Conceptual/iphone_development/135-Unit_Testing_Applications/unit_testing_applications.html

eonil
  • 83,476
  • 81
  • 317
  • 516
  • 2
    Mate, this is exactly copy-pasted from apples documentation: _"Because application tests **can run in simulators and on devices**, you can also use these tests to perform hardware testing, such as getting the location of the device in your application."_ Hence Application Tests Are working in the simulator as well and not only on the device. – Paul Peelen Nov 15 '11 at 12:54