Skip to content

Instantly share code, notes, and snippets.

@atcruice
Created March 26, 2014 12:24
Show Gist options
  • Save atcruice/9781963 to your computer and use it in GitHub Desktop.
Save atcruice/9781963 to your computer and use it in GitHub Desktop.
A guide for using the Google Testing Framework with Xcode 5 on Mac OS X

XcodeGuide (updated)

A guide for using the Google Testing Framework with Xcode 5 on Mac OS X

Assumed knowledge

  • basic command line chops
  • elementary Xcode 5 skills: project creation, target creation, scheme management/editing and building

Quick start

  1. Download the source from the website using this command:

     svn checkout http://googletest.googlecode.com/svn/trunk/ googletest-read-only
    
  2. Open up the gtest.xcodeproj in the googletest-read-only/xcode/ directory and build the gtest.framework. More than likely this will place a built gtest.framework at a location similar to:

     ~/Library/Developer/Xcode/DerivedData/gtest-xxxxxxxx.../Build/Products/Debug/
    
  3. Create a new "Command Line Tool" target in your Xcode project called something like "UnitTests"

  4. Add the gtest.framework to your project and add it to the "Link Binary with Libraries" build phase of "UnitTests."

    Note that when you add the framework to the project you have the option of selecting which targets to add it to. If you selected the UnitTests target you've just saved yourself the Link-Binary-with-Libraries part of the step. Feel free to check it anyway to learn how to perform this action any time:

    1. In the Project Navigator (command-1), select your project. The Standard Editor (command-return) should show you the project settings tab; select the "Build Phases" tab.
    2. Expand the "Link Binary with Libraries" section.
    3. If gtest.framework is not already listed, drag in onto the list.
  5. Add your unit test source code to the "Compile Sources" build phase of "UnitTests"

    If you don't know where this is, follow the sub-steps of step 4 above to find the project "Build Phases" tab, then expand the "Compile Sources" section (right above "Link Binary with Libraries"). Just drag any .c/.cpp files you want to be able test onto the list (ensure the UnitTests.cpp is listed as well).

  6. Ensure "UnitTests" is the current selected scheme and edit the "UnitTests" scheme (command-shift-,) and add an environment variable (under the Arguments tab) named "DYLD_FRAMEWORK_PATH" with a value equal to the path to the framework containing the gtest.framework relative to the compiled executable.

     /User/username/Library/Developer/Xcode/DerivedData/gtest-xxxxxxxx.../Build/Products/Debug/
    

    If you chose to add a reference to the framework to your project this will be an absolute path. If you chose to copy the framework into your project you'll need to adjust the path accordingly.

  7. Write your tests in UnitTests.cpp, and Run!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment