Integrating GoogleTest with Xcode 4

I been thinking and reading about Unit Testing for a long while, but never dared to actually implement it in any of my projects (both in work and hobby projects) for various reasons the most obvious one being ‘laziness’.

But all of a sudden I got enlightened yesterday and been sincerely reading about UnitTest++, googletest and some other testing frameworks and decided to go with googletest for obvious reasons.

I downloaded the framework but found that the official xcode integration guide is out of date now and I couldnt easily find any other guides to integrate it with xcode 4 (mine is 4.2). So here I am writing one….

Building the gtest framework

  • Download the framework.
  • Extract the zip.
  • Open the xcode project @ gtest-1.6.0/xcode/gtest.xcodeproj
  • Select gtest-framework Scheme and build the project.
  • If you get the errorThe run destination My Mac 64-bit is not valid for Running the scheme ‘gtest-framework’. The scheme ‘gtest-framework’ contains no buildables that can be built for the SDKs supported by the run destination My Mac 64-bit. Make sure your targets all specify SDKs that are supported by this version of Xcode“, try setting the following for both `Project` and `gtest-framework` Target:
    • `Base SDK` to any proper available SDK.
    • `Compiler for C/C++/Objective-C` to any proper compiler. LLVM GCC worked for me.
gtest-framework-scheme

scheme selection for gtest-framework

  • On successfull compilation, right click on gtest.framework in Products folder in Navigator and select ‘Show in Finder’
gtest-framework-products

build products for gtest-framework

 

  • Copy the gtest.framework folder to a common location (mine is /Users/saiy2k/projects/gtest-1.6.0/products)

Integrating with Real Project

  • Open the project in which testing capabilities need to be added.
  • Click on the project name in Navigator.
  • Create new target by clicking ‘Add Target’. (pic)
  • select Mac OS X –> Application –> Command Line Tool
  • Enter Product Name and other fields and click finish.
  • Selecting the newly created Target and goto Build Phases tab.
  • Add gtest.framework in our common location to ‘Link Binary with Libraries’
gtest-framework-target-build-phases

adding new target and adding gtest framework to it

Writing Test Case and Testing

In the Project Navigator, you should be having a new folder in the name given to the Target project. Under that project create a header file ‘TestCase1.h’ with the following contents:

TEST(SampleTest, Zero) {
    EXPECT_EQ(0, 0);
}

TEST(SampleTest, Positive) {
    EXPECT_EQ(1, 1);
    EXPECT_EQ(6, 3*2);
    EXPECT_EQ(20, 10*2);
}

TEST(SampleTest, Negative) {
    EXPECT_EQ(-1, -1);
    EXPECT_EQ(-2, -2);
    EXPECT_EQ(-3, -3);
}

Now select the scheme for New Test Target and run it and check the log for output of the test run. For writing actual test cases, refer the googletest wiki.

6 thoughts on “Integrating GoogleTest with Xcode 4

  1. Thanks for your tutorial.
    I get stuck at the first step though.
    The run destination My Mac 64-bit is not valid for Running the scheme ‘gtest-framework’.
    Can you help me with this? :)

    • Click on Project Name in Navigator to open Project Settings Page. Do the following for both Project and gtest-framework Target.
      Assign any proper value to `Compiler for C/C++/Objective-C` and then build. `LLVM GCC 4.2` worked for me.

      • I’m getting that error as well and I tried you advice but the error remains. I don’t know what to do.

        Heres the full error if it helps.
        “The run destination My Mac 64-bit is not valid for Running the scheme ‘gtest-framework’. The scheme ‘gtest-framework’ contains no buildables that can be built for the SDKs supported by the run destination My Mac 64-bit. Make sure your targets all specify SDKs that are supported by this version of Xcode.”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>