如何使用Xcode 5从命令行运行xctest?

Chr*_*ahl 16 xcode unit-testing xctest

我找到了一个名为"xctest"的命令行工具,它显然可以在你的项目中运行单元测试.此可执行文件位于此处

/Applications/Xcode.app/Contents/Developer/usr/bin/xctest
Run Code Online (Sandbox Code Playgroud)

当我尝试在我的xctest包上运行此可执行文件时,我正在使用:

$ ./xctest /Users/myusername/Library/Developer/Xcode/DerivedData/MyApp-abcdefghijklmnop/Build/Products/Debug/MyAppTests.xctest
Run Code Online (Sandbox Code Playgroud)

但是,我得到以下输出:

Test Suite '(null)' started at 2013-11-14 21:16:45 +0000
Test Suite '(null)' finished at 2013-11-14 21:16:45 +0000.
Executed 0 tests, with 0 failures (0 unexpected) in 0.000 (0.001) seconds
Run Code Online (Sandbox Code Playgroud)

据我所知,xctest没有手册页,但在命令行输入./xctest会产生:

Usage: xctest [--test Self | All | None | <TestCaseClassName/testMethodName>] <path of unit to be tested>
Run Code Online (Sandbox Code Playgroud)

特别是,我希望能够在测试类中测试一个特定的方法,这就是为什么我想使用这个xctest命令.

我确实看到有一种方法可以从命令行运行所有测试,如:

$ xcodebuild test -scheme MyApp
Run Code Online (Sandbox Code Playgroud)

这将运行所有单元测试并正常工作(我看到我的单元测试结果,与使用xctest时不同).但我对能够从命令行运行单个测试方法感兴趣,例如:

$ ./xctest --test MyAppTests/testExample  /Users/myusername/Library/Developer/Xcode/DerivedData/MyApp-abcdefghijklmnop/Build/Products/Debug/MyAppTests.xctest
Run Code Online (Sandbox Code Playgroud)

Mat*_*ens 6

尽管使用消息说-XCTest是您需要的参数:

xctest -XCTest MyAppTests/testExample testbundle.xctest
Run Code Online (Sandbox Code Playgroud)

要直接调用xctest工作,您可能还需要设置DYLD_FRAMEWORK_PATHDYLD_LIBRARY_PATH构建产品目录.通常,您需要使用与Xcode相同的参数和环境,您可以通过在其中一个测试中放置断点,通过Xcode运行它们,然后打印出值argumentsenvironmentfor 来看到这一点[NSProcessInfo processInfo].

为了避免弄乱所有注意事项,您还可以在Xcode中修改方案以仅运行特定测试.在Product> Scheme> Edit Scheme下,选择Test操作并展开测试包.您可以使用复选框选择要运行的测试,然后xcodebuild的测试操作将仅运行这些测试.