Adding Unit Tests to an already existing project?

Randall picture Randall · Jul 28, 2011 · Viewed 23.4k times · Source

When you create an XCode 4 project, it asks you if you want to include unit testing. But how do you add it to a project that's been created without them?

I started a project with Unit Tests to try to figure it out, and it looks like the only difference between it and my existing project are the Test.h and Test.m files in the new project.

But when I move them into my old project and try to build, it says no such file or directory: SenTestingKit/SenTestingKit.h. That looks like a framework to me, but if I go to build phases and try to add the framework, there isn't one available called SenTestingKit :( And the new project only links the usual suspects: UIKit, CoreGraphics, and Foundation, but no SenTestingKit.

Answer

akashivskyy picture akashivskyy · Jul 28, 2011

Answer updated for Xcode 10.2.

Glad you asked this question — it's never too late to add tests to a project!

  1. Open your targets panel by selecting your project in the navigator,
  2. Click the + button that's at the bottom of the target list,
  3. Select iOS Unit Testing Bundle under Test section in iOS tab,
  4. Provide the required information and click Done.

It's that simple. Xcode will generate a target, some boilerplate files such as Info.plist and an initial test case file for you. Happy testing!