Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking error for unit testing with XCode 4?

Tags:

I want to write some logic unit tests for classes in my XCode application. In Xcode 4, I clicked on the project name in the Project Navigator, and from the bottom clicked Add Target. I chose "Cocoa Touch Unit Testing Bundle" under Other, give the new target a "product name" of "tests", and finish.

Because the class I want to test is compiled as part of my existing application target, for my new "tests" target I immediately go to the Build Phases tab and add my existing application target as the only target dependency.

I then go to the created tests.m file, import the class I want to test (below it's ReleasePlanManager, and call one of its methods. But the linker fails with an error like:

Undefined symbols for architecture i386:   "_OBJC_CLASS_$_ReleasePlanManager", referenced from:       objc-class-ref in tests.o ld: symbol(s) not found for architecture i386 collect2: ld returned 1 exit status 

So the class cannot be found, even though (from my understanding) adding the application target (which it is a part of) should be sufficient?

Any help would be greatly appreciated. Thanks!

like image 938
shadowmatter Avatar asked Jun 23 '11 21:06

shadowmatter


People also ask

How do I set unit tests in Xcode?

To add a unit test target to an existing Xcode project, choose File > New > Target. Select your app platform (iOS, macOS, watchOS, tvOS) from the top of the New Target Assistant. Select the Unit Testing Bundle target from the list of targets.

How do I get unit test coverage in Xcode?

Enabling Code Coverage in Xcode Code coverage is enabled in the scheme editor. Click the Covered scheme and choose Edit Scheme.... Select Test on the left and check the checkbox Gather coverage data. That is it.

How do I enable unit testing?

Turn live unit testing from the Test menu by choosing Test > Live Unit Testing > Start.

How do I enable code coverage in Xcode?

Running Tests and Coverage Locally in Xcode To enable code coverage, click the scheme editor in the toolbar. Select the CodecovDemo scheme and choose Edit Scheme. Select the Test action on the left. Check the Code Coverage box to gather coverage data.


1 Answers

Your test bundle needs extra settings:

  • Set Bundle Loader to $(BUILT_PRODUCTS_DIR)/AppName.app/AppName (replacing AppName in both places with your app's name)
  • Set Test Host to $(BUNDLE_LOADER)

(If you create a project from scratch and enable unit tests, these are set up for you. But if you add a unit test bundle to an existing project, they're not.)

like image 163
Jon Reid Avatar answered Nov 07 '22 09:11

Jon Reid