Kai Huppmann
Kai Huppmann

Reputation: 10775

Why don't tests compile in Xcode?

I tried to add unit tests to an (very long) existing project in Xcode. I created an test target (normal app-target is in "target dependencies") and the automatically created test ran without problems. Then I tried to do some real testing on one of my (not so complicated) model classes. I added this one to the "compile sources" build phase, but get several compiler errors. Some of those can be solved by importing and compiling other classes of my project, but others - and that's really irritating - I can only get rid of by adding import statements like <UIKit/UIKit.h> to existing project classes, which compile well since ages without these statements (and still do in the normal app-target). So my main question is:

What's best practice to start with unit test on an very old project?

But I also like to know:

If I want to make it run, do I really have to add all my 250 classes to "compile sources" and adding every single library of the app=target to the test-target as well? And if so,is there a quick way to duplicate these parts of the app target?

I searched a lot on the internet, but no tutorial or manual seems to address my problem.

I'm using Xcode 6.

Upvotes: 1

Views: 1721

Answers (2)

Kai Huppmann
Kai Huppmann

Reputation: 10775

I finally managed everything. If you have the same problem simply follow the steps in this post. It's rather old and was written for Xcode 4, but it's still the right way.

Actually I did read it before I asked this question, but unfortunately after I tried things on my own. My fault was trying to fix the linker errors by adding my app classes to the "Compile Sources"-section of the build phases of my test target, which lead to new errors, which I tried to fix by adding more app-sources to the build phase and linking libraries to the test binary and it all made things worse. So:

  • Just add your test classes to the "Compile Sources" build phase of your test target. Don't add any app sources here.
  • Don't link any binaries within the test target, that you already linked to the app target.
  • Be very sure to set "Symbols Hidden By Default"-entry in the build settings of your app target to "No".

Upvotes: 1

Midhun MP
Midhun MP

Reputation: 107121

You can add unit test bundle to existing project by using:

Unit Test Bundle

In my opinion, if you want to write unit test for independent class, adding that class to Compile Source is a better approach.

But if your class imports other class and if it's like a chain, then adding whole source is a better and easy way to avoid compile errors.

Upvotes: 0

Related Questions