Adding Unit Tests to an already existing project?

Viewed 26438

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.

7 Answers

One more variant using Test navigator

Xcode version 10.2.1

  1. Open Test navigator(⌘ Command + 6)
  2. + at the bottom
  3. New Unit Test Target...
  4. Choose options

enter image description here

Do not forget add next import to test internal[About] elements

  • #import "myClass.h" for Objective-C
  • @testable import module_name for Swift

[Updated for Xcode 10.2]:

  1. Open your Xcode project
  2. Open targets
  3. Click "Add Target" button
  4. Go to "Test" section
  5. Select "iOS Unit Testing Bundle"
Related