63

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.

Randall
  • 2,083
  • 2
  • 18
  • 20

7 Answers7

106

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!

akashivskyy
  • 44,342
  • 16
  • 106
  • 116
  • 1
    To clarify for the original poster, you need to add a unit testing bundle target to your project. The unit testing bundle is in the Other group under both iOS and Mac OS X. – Swift Dev Journal Jul 29 '11 at 03:35
  • @Mark: Thanks, that's what I was talking about. – akashivskyy Jul 29 '11 at 07:50
  • I added the target, but still when I add: new file -> Objective C Test Case Class, and try to compile, it says cannot find SenTestingKit/SenTestingKit.h. – Randall Jul 29 '11 at 13:54
  • 2
    Make sure the .m file for the test case class is a member of the unit testing target. When you add a test case class to a project, Xcode's default behavior is to add the class to the application target, not the unit testing target. Select the test case class in the project navigator and open the file inspector to see the file's target membership. – Swift Dev Journal Jul 29 '11 at 19:36
  • @kashiv, my unit test target was working fine until I added new files into project, after that it wont work on hudson (automatic build and test). my build is still success and can run the main target and unittest both in xcode, but its not working in hudson. Do I need to add those files to test target as well? – Jay Vachhani Oct 28 '11 at 18:47
  • @JayVachhani: You should check if your test .m files are members of text target. – akashivskyy Oct 28 '11 at 19:03
  • @Kashiv Yes, it is all same nothing change in unittest folder. – Jay Vachhani Oct 28 '11 at 20:59
19

Also, if you want to be able to do cmd-U to run your tests then delete the scheme that was created for the test bundle and instead edit your main application scheme and add the test bundle in the Test configuration. See this screenshot:

Run tests with cmd-U in xcode

Simon Bengtsson
  • 7,573
  • 3
  • 58
  • 87
Lee Probert
  • 10,308
  • 8
  • 43
  • 70
  • In Xcode 11, when a test bundle target is added, then such test bundle as already listed and enable in the test section of the currently active schema. A separate test schema is not generated. – marc-medley Nov 14 '19 at 00:43
7

Xcode 7 update

Method one

File > New > Target...

Choose iOS Unit Testing Bundle. (If you want to add UI Testing then choose the UI Testing Bundle.)

Method two

Click your project name in the Project Navigator. You will see TARGETS listed. At the bottom of the screen press the plus (+) button and add the iOS Unit Testing Bundle.

enter image description here

The test targets in this image have already been added, but you can see where the add button is.

Related

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
7

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
yoAlex5
  • 29,217
  • 8
  • 193
  • 205
5

This is pretty descriptive how-to guide: Adding Unit Tests to an existing iOS project with Xcode 4

jki
  • 4,617
  • 1
  • 34
  • 29
1

Some more tips to the correct answer:

In XCode 6 it is much easier now since you don't need to fix any build settings.

Change the bundle id on a test target to the correct one, if needed, by clicking the target -> Info -> Bundle Identifier.

Press CMD + U and your tests will run.

If you use CocoaPods (which is likely), you should also add Pods to the Configurations of your Project.

enter image description here

Denis Kutlubaev
  • 15,320
  • 6
  • 84
  • 70
1

[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"
Swan
  • 305
  • 2
  • 6