Unit Testing Applications in Cocoa

| | Comments (0)

http://chanson.livejournal.com/tag/unit+testing is a wonderful resource for getting unit tests up and running in cocoa (esp under the debugger). One of the comments was a gem and I pulled it out and fixed it up to work a bit better... This converts your project to run your application's unit tests under gdb. I can't get it to work under XCode's script menu, but it should work fine under the system wide script menu.

(* 
To debug the unit tests, in the "General" tab of the executable info window, the "Working Directory" must be set to "Build Products directory"
Modify the testFrameworkPath for your system
*)
set testFrameworkPath to "/Developer/Library/Frameworks/SenTestingKit.framework"

tell application "Xcode"
  tell project of active project document
    set projectName to name
    set executablePath to "$(BUILT_PRODUCTS_DIR)/" & projectName & ".app/Contents/MacOS/" & projectName
    tell executable name
      make new launch argument with properties {name:"-SenTest All", active:true}
      make new environment variable with properties {name:"DYLD_FALLBACK_FRAMEWORK_PATH", value:"$(DEVELOPER_LIBRARY_DIR)/Frameworks", active:true}
      make new environment variable with properties {name:"XCInjectBundleInto", value:(projectName & ".app/Contents/MacOS/" & projectName), active:true}
      make new environment variable with properties {name:"XCInjectBundle", value:"Unit Tests.octest", active:true}
      make new environment variable with properties {name:"DYLD_INSERT_LIBRARIES", value:"$(DEVELOPER_LIBRARY_DIR)/PrivateFrameworks/DevToolsBundleInjection.framework/DevToolsBundleInjection", active:true}
    end tell
    set unitTestTemplate to target template "Cocoa/Unit Test Bundle"
    make new target at end of targets with data unitTestTemplate with properties {name:"Unit Tests"}
    tell root group
      make new group with properties {name:"Test Cases"} at beginning of groups
    end tell
    tell group "Linked Frameworks"
      set testFrameworkFile to make new file reference with properties {full path:testFrameworkPath, name:"SenTestingKit.framework"}
    end tell
    add testFrameworkFile to (get link binary with libraries phase of target "Unit Tests")
    set value of build setting "BUNDLE_LOADER" of build configuration "Debug" of target "Unit Tests" to executablePath
    set value of build setting "TEST_HOST" of build configuration "Debug" of target "Unit Tests" to "$(BUNDLE_LOADER)"
    set active target to target "Unit Tests"
  end tell
end tell

activate application "Xcode"
tell application "System Events"
  tell process "Xcode"
    tell outline 1 of scroll area 1 of splitter group 1 of group 1 of window 1
      tell (first row whose value of text field 1 of group 1 = "Targets")
        set targetGroupDisclosureTriangle to UI element 1 of group 1
        if value of targetGroupDisclosureTriangle = 0 then
          perform action "AXPress" of targetGroupDisclosureTriangle
        end if
      end tell
      select (first row whose value of text field 1 of group 1 = "Unit Tests")
    end tell
    perform action "AXPress" of menu item "Get Info" of menu 1 of menu bar item "File" of menu bar 1
    tell window "Target “Unit Tests” Info"
      click radio button "General" of tab group 1
      perform action "AXPress" of button 1 of splitter group 1 of tab group 1
      tell sheet 1
        select (first row of outline 1 of scroll area 1 whose value of text field 1 = projectName)
        click button "Add Target"
      end tell
      click button 1
    end tell
  end tell
end tell

Leave a comment

About this Entry

This page contains a single entry by zenspider published on October 27, 2009 1:56 PM.

osx_keychain version 1.0.0 has been released! was the previous entry in this blog.

Apparently I Suck is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Pages

Powered by Movable Type 4.32-en