How to add Unit Test Project in an existing Objective-C project in XCode 4

There are many posts in web regarding this topic. My post is like reinventing the wheel. In this post I will try to guide you to add unit test project in an existing Objective-C project in Xcode 4, step-by-step.

Before the journey I want to state that if you are planning to start your new project then you can add your test project at the time of creation. That process will be discussed in separate post. So let’s start.

Considering your existing project name <My_Apps>.

  • Open your <My_Apps> in your XCode.
  • Select the Projects Blue icon in the Project Navigator panel.
  • Click Add Target resides at the bottom panel.

Add Test Project

  • Select Other of iOS section from the left panel of the pop-up appeared.
  • From list of options in the right panel select Cocoa Touch Unit Testing Bundle.
  • Click Next.

Select Test Project

  • Enter the Product Name , say <My_Apps_Test> and Click Finish.

Project Name

A test project with your given name should be added in the Project Navigator. In your test project there should be 2 files added by default – My_Apps_Test.h & My_Apps_Test.m.

Let’s check the files that XCode created for us.

Open My_Apps_Test.h. In this file the test framework will be imported by default with empty @interface declaration as below:

        #import <SenTestingKit/SenTestingKit.h>
        @interface My_Apps_Test : SenTestCase

        @end

Open My_Apps_Test.m and you should be able to see 3 functions XCode created for you – setUp, tearDown & testExample.

WAIT…… you are not done yet.

Click on the Unit Test Project icon again and follow the steps:

  • Click Build Phases tab.
  • Expand Target Dependencies.

Target-Dependencies

  • Click +.
  • In the pop-up appeared, you should be able to see your project. Select it and click Add.
  • Your project should be added to the Target Dependencies section.

Let us check the Compile Source section immediately under the Target Dependencies section. In this section the .m extension file of the test project <My_Apps_Test> file should be added by default. Note if your test project directory have more files then all the .m extensions of the files will be added by default.

Compile-Source

We are left out with one more task to do for Build Phases tab. Expand Link Binary With Libraries. In this section notice that the SenTestingKit’s library file is highlighted with red color and no yellow icon will be shown beside it as others – SenTestingKit.Framework. Now let us fix this problem with the following three (03) steps:

  • Select SenTestingKit.Framework.
  • Click situated below of the section.
  • Now click + at the left side of .
  • From the pop-up select the SenTestingKit.Framework again. (Good idea is to type the framework name in the search field.)
  • Click Add.
  • Check the Library list. The framework should be added properly and the yellow icon beside the framework should be shown properly.

Now check the Project Navigator. Newly added SenTestingKit framework is added under the Project. If you like you can drag it to the Frameworks directory in the Project Navigator panel.

Next step will be to open the Frameworks directory. Here you will have another SenTestingKit.Framework highlighted in red color with no yellow icon. Remove it with no worries.

Next Step will be adding build settings and for that click on the Build Settings tab right beside Build Phases tab. After the tab opens add the following value to their proper fields, exactly in the same order as the steps defines:

  • Linking > Bundle Loader > $(BUILT_PRODUCTS_DIR)/<YOUR APP NAME>.app/<YOUR APP NAME>. According to my tutorial <YOUR APP NAME> will be replaced with My_Apps.
  • Unit Testing > Test Host > $(BUNDLE_LOADER)
  • Apple LLVM compiler 4.1 – Code Generation > Symbol Hidden By Default > NO.

OK now we are done with adding our test project. Let us make a test run. To Test we need to click: Product > Test  or  U.

OOPS!!! Now why the Test option is disabled? Don’t worry one simple step will solve this problem. From scheme, at the top left of the XCode just beside the RUN and STOP buttons, click My_Apps. From pop-up list select My_Apps_Test > iPhone 6.0 Simulator, considering you are trying to test in iPhone Simulator. It is OK to select any other Simulator from simulator available list for the target – My_Apps_Test.

Now again try to Test the project. This time you should be able to run the test.

After the test is done, test result will show FAIL and will show the message that is set in the My_Apps_Test.m file – Unit tests are not implemented yet in My_Apps_Test. As no test has been implemented yet in your test project thus the test shows the fail message, which is expected.

Test Fail

That’s all folks!!

~::~     Happy Testing     ~::~

Advertisements

14 thoughts on “How to add Unit Test Project in an existing Objective-C project in XCode 4

  1. Pingback: nicegood
  2. Pingback: jacketbh15
  3. Pingback: hotblogvr3
  4. Pingback: blogrblogs
  5. Pingback: qefblog40
  6. Pingback: zokblogwb6
  7. Pingback: myblogzn3

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s