由于otest错误,无法从命令行运行Xcode单元测试

时间:2013-06-05 05:29:02

标签: ios xcode unit-testing

我正在尝试从命令行运行单元测试,但是当它们在Xcode 4.6中运行完全正常时它们会失败。我无法弄清楚失败的原因是什么:

这是我正在使用的命令:

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).

如果有办法打印出callstack(英文版),我可以找出错误的来源。

1 个答案:

答案 0 :(得分: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

应用测试与逻辑测试
为新项目创建的单元测试目标是应用程序单元测试。它通过在构建设置中设置BUNDLE_LOADER和TEST_HOST将测试代码注入应用程序。 您从“新建目标”菜单创建的测试目标是逻辑测试。逻辑测试独立于您的应用程序。

这是我的Bundle Loader和测试主机值。

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