14

I've created an iOS unit test target for doing logic tests following the steps provided in Apple's documentation.

However my build fails and i get the following error:

Undefined symbols:
"_OBJC_CLASS_$_MyClass", referenced from: objc-class-ref-to-MyClass in LogicTests.o ld: symbol(s) not found collect2: ld returned 1 exit status

Ordinarily, if I wanted to use my static library within an application I would include the library.a file, and the headers(including the MyClass.h file...). Is something additional required to run logic tests on a static library WITHIN that same project if my test cases are utilizing MyClass.h ?

Tjhanks

Patrick
  • 1,079
  • 1
  • 9
  • 14
  • Good question. Bad answer sir. The truth is, due to the nature of a static library, you can't perform application tests. – Daniel Jun 29 '12 at 19:08

4 Answers4

16

Due to the nature of static libraries, you can't perform application tests, which by the sound of it is what you are trying to do. However, you can perform logic tests.

You were correct in your observation about unit testing in the client application.

The Xcode template optionally includes unit tests, but if you go to the build settings for that unit test you will see it doesn't specify a test host or bundle loader. This is because of the nature of static libraries. They are not applications, they are libraries - so you can do logic tests, you cannot do application tests.

Application tests you may wish to perform on your static library may include the following scenario:

My library creates an SQLite database at runtime, I wish to perform a unit test to check everything is inserting and/or updating as expected.

In order to test this with unit tests, one must create another application which includes or otherwise is dependant of your library. This application then includes your library and application tests may then be set up there.

Michael Dorst
  • 8,210
  • 11
  • 44
  • 71
Daniel
  • 23,129
  • 12
  • 109
  • 154
  • can you explain this statement ? "Due to the nature of static libraries, you can't perform application tests" . – namanhams Nov 14 '18 at 08:02
4

Apple has a sample up (UnitTests) that shows how to do this: https://developer.apple.com/library/ios/#samplecode/UnitTests/Introduction/Intro.html#//apple_ref/doc/uid/DTS40011742

quellish
  • 21,123
  • 4
  • 76
  • 83
3

I actually just solved it. I had to copy all of the .m files in my project to the LogicTest target's 'Compile Sources'. As well as add the frameworks the sources reference to the 'Link Binary With Libraries' section of the target.

I hope this helps others

Patrick
  • 1,079
  • 1
  • 9
  • 14
  • 3
    Actually, for a static library, you're better off not including your sources in your test target. You do have add your .m files if you're _not_ working with a static library. – Jon Reid Dec 29 '10 at 07:06
  • Jon, that's kind of confusing, can you be a bit clearer? Why would you be better off? – Billy Gray Apr 24 '12 at 15:43
  • It's a pain because every time you create a new file, you have to remember to add it to two targets. Xcode lets you keep your test code separate from the code under test, linking the former to the latter. – Jon Reid Apr 30 '12 at 19:52
  • Copying and compiling the .m files defeats the purpose of having a static library in the first place. Basically, logic tests are not possible using XCTests. Application tests are however possible. The sample app mentioned by @quellish makes this clear, particularly in the readme file. – dagnytaggart Jul 01 '14 at 18:31
2

Yes, Xcode 4.2 has a template for Cocoa Touch Static Library with tests.

ma11hew28
  • 121,420
  • 116
  • 450
  • 651