2013-06-05 53 views
0

我試圖從命令行運行單元測試,但它們在Xcode 4.6中運行完美時失敗。在我的測試無法從命令行運行Xcode單元測試由於錯誤

xcodebuild -sdk iphonesimulator -project myproject.xcodeproj 
    -scheme 'MyProjectApplicationTests' 
    -configuration Debug clean build 
    RUN_UNIT_TEST_WITH_IOS_SIM=YES TEST_AFTER_BUILD=YES TEST_HOST='' 

我沒有NSURL的使用情況或「initFileURLWithPath」:我無法弄清楚什麼是失敗的原因:

下面是我使用的命令。這裏的錯誤:

/Applications/Xcode.app/Contents/Developer/Tools/RunPlatformUnitTests.include:412: note: Started tests for architectures 'i386' 
Run unit tests for architecture 'i386' (GC OFF) 
/Applications/Xcode.app/Contents/Developer/Tools/RunPlatformUnitTests.include:419: note: Running tests for architecture 'i386' (GC OFF) 
2013-06-04 22:09:57.605 otest[85321:707] Unknown Device Type. Using UIUserInterfaceIdiomPhone based on screen size 
2013-06-04 22:09:57.624 otest[85321:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter' 
*** First throw call stack: 
(0xa05012 0x71ce7e 0xa04deb 0x2a9b1 0x2a93b 0x4d3c5c9 0x71d7cf 0x724a0d 0x71baeb 0x71be22 0x72e0e1 0x2010879a 0x20106ef5 0x20107124 0x20107196 0x2010624c 0x201063da 0x7305c8 0x2342 0x25ef 0x268c 0x2001 0x1f71) 
libc++abi.dylib: terminate called throwing an exception 
/Applications/Xcode.app/Contents/Developer/Tools/RunPlatformUnitTests.include: line 415: 85321 Abort trap: 6   "${THIN_TEST_RIG}" "${OTHER_TEST_FLAGS}" "${TEST_BUNDLE_PATH}" 
/Applications/Xcode.app/Contents/Developer/Tools/RunPlatformUnitTests.include:451: error: Test rig '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/Developer/usr/bin/otest' exited abnormally with code 134 (it may have crashed). 

如果有辦法,甚至打印出調用堆棧(英文),我也許可以找出錯誤所在距離。

+0

你試圖運行應用程序測試而不是邏輯測試,對嗎?你有'ios-sim'正確安裝? – mAu

+1

我有iOS模擬器應用程序,但是當我在命令行輸入'ios-sim'時,沒有任何東西。清單/Applications/Xcode.app/Contents/Applications顯示我有'iPhone Simulator.app'。 –

+0

至於應用程序測試與單元測試的區別,這是一個作爲測試包創建的目標。我不確定是什麼把測試看作是「邏輯測試」和「應用測試」 - 整個概念對我來說似乎很不合理。無論如何,測試都來自SenTestCase。 –

回答

8

問題是xcodebuild本身不支持模擬器中的應用程序測試。要從命令行在模擬器中運行應用程序測試,需要開放源代碼實用程序ios-sim和測試目標運行腳本的tweek。

1)安裝IOS-SIM https://github.com/phonegap/ios-sim

$ curl -L https://github.com/phonegap/ios-sim/zipball/1.9.0 -o ios-sim-1.9.0.zip 
$ unzip ios-sim-1.9.0.zip 
$ cd phonegap-ios-sim-538ef1a/ 
$ sudo rake install prefix=/usr/local/ 

2)編輯您的測試目標上運行的腳本。這個腳本來自Atlassian的神話般的文檔。我喜歡JIRA,Stash,Confluence以及我最新的好朋友SourceTree的Atlassian製造商。充分披露他們收購SourceTree,然後開發者免費發佈。

替換現有的:

# Run the unit tests in this test bundle. 
"${SYSTEM_DEVELOPER_DIR}/Tools/RunUnitTests" 

有了這個(來源:https://confluence.atlassian.com/display/BAMBOO/Xcode

if [ "$RUN_UNIT_TEST_WITH_IOS_SIM" = "YES" ]; then 
    test_bundle_path="$BUILT_PRODUCTS_DIR/$PRODUCT_NAME.$WRAPPER_EXTENSION" 
    ios-sim launch "$(dirname "$TEST_HOST")" --setenv DYLD_INSERT_LIBRARIES=/../../Library/PrivateFrameworks/IDEBundleInjection.framework/IDEBundleInjection --setenv XCInjectBundle="$test_bundle_path" --setenv XCInjectBundleInto="$TEST_HOST" --args -SenTest All "$test_bundle_path" 
    echo "Finished running tests with ios-sim" 
else 
    "${SYSTEM_DEVELOPER_DIR}/Tools/RunUnitTests" 
fi 

enter image description here

3)運行測試

xcodebuild \ 
    -sdk iphonesimulator6.1 \ 
    -project DC\ Wire\ Sizer.xcodeproj \ 
    -target DC\ Wire\ Sizer\ Tests \ 
    -configuration Debug \ 
    RUN_UNIT_TEST_WITH_IOS_SIM=YES 

... 

Test Suite 'All tests' finished at 2013-06-06 16:03:35 +0000. 
Executed 104 tests, with 0 failures (0 unexpected) in 0.991 (1.063) seconds 

Finished running tests with ios-sim 
Showing first 200 notices only 


** BUILD SUCCEEDED ** 

有用的命令:

xcodebuild聯編-showsdks

$ xcodebuild -showsdks 
OS X SDKs: 
    Mac OS X 10.7     -sdk macosx10.7 
    OS X 10.8      -sdk macosx10.8 

iOS SDKs: 
    iOS 6.1       -sdk iphoneos6.1 

iOS Simulator SDKs: 
    Simulator - iOS 4.3    -sdk iphonesimulator4.3 
    Simulator - iOS 5.0    -sdk iphonesimulator5.0 
    Simulator - iOS 5.1    -sdk iphonesimulator5.1 
    Simulator - iOS 6.0    -sdk iphonesimulator6.0 
    Simulator - iOS 6.1    -sdk iphonesimulator6.1 

xcodebuild聯編-list -project

$ xcodebuild -list -project DC\ Wire\ Sizer.xcodeproj 
Information about project "DC Wire Sizer": 
    Targets: 
     DC Wire Sizer 
     DC Wire Sizer Tests 
     In Work Unit Test 

    Build Configurations: 
     Debug 
     Release_AppStore 
     Release_TestFlight 

    If no build configuration is specified and -scheme is not passed then "Release_AppStore" is used. 

    Schemes: 
     DC Wire Sizer 
     DC Wire Sizer - App Store 
     InWorkUnitTest 

應用試驗VS邏輯測試
爲新項目創建的單元測試目標是應用程序單元測試。它通過在構建設置中設置BUNDLE_LOADER和TEST_HOST將您的測試代碼注入到應用程序中。 您從New Target菜單創建的測試目標是邏輯測試。邏輯測試獨立於您的應用程序。

這是我的Bundle加載器和測試主機值。

Bundle Loader: $(BUILT_PRODUCTS_DIR)/DC Wire Sizer.app/DC Wire Sizer 
Test Host:  $(BUNDLE_LOADER) 

潛在的問題:如果安裝的Xcode的多個版本。然後你需要檢查你的xcode選擇設置:

gdunham: ~$ xcode-select -print-path 
/Applications/Xcode.app/Contents/Developer 
+0

我仍在運行10.8.3,並且10.8.4用戶看到的模擬器沒有任何崩潰。測試包是在某個時間之前創建的(不確定在哪個版本的Xcode中) - 是否有某種方法可以辨別項目是否具有應用程序或邏輯測試? –

+0

是,如果設置了Bundle Loader,請檢查Linking部分中的測試目標的構建設置。這是一個應用程序單元測試。 – GayleDDS

+0

@PaulHansen更新了答案,以檢查您的xcode選擇設置。 – GayleDDS