2013-11-14 53 views
14

我发现了一个名为“xctest”的命令行工具,它显然可以在您的项目中运行单元测试。此可执行住在这里:如何使用Xcode 5从命令行运行xctest?

/Applications/Xcode.app/Contents/Developer/usr/bin/xctest 

当我尝试在我的xctest束运行这个可执行文件,我使用的是:

$ ./xctest /Users/myusername/Library/Developer/Xcode/DerivedData/MyApp-abcdefghijklmnop/Build/Products/Debug/MyAppTests.xctest 

不过,我得到以下输出:

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 

据我所知,没有关于xctest的手册页,但是在命令行中输入./xctest得到:

Usage: xctest [--test Self | All | None | <TestCaseClassName/testMethodName>] <path of unit to be tested> 

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

我看到有从运行像在命令行中的所有测试方式:

$ xcodebuild test -scheme MyApp 

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

$ ./xctest --test MyAppTests/testExample /Users/myusername/Library/Developer/Xcode/DerivedData/MyApp-abcdefghijklmnop/Build/Products/Debug/MyAppTests.xctest 

回答

5

尽管什么用法消息说-XCTest是你需要的参数:

xctest -XCTest MyAppTests/testExample testbundle.xctest 

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

为了避免混淆所有注意事项,您还可以修改Xcode中的方案以仅运行特定的测试。在产品>方案>编辑方案下,选择测试操作并展开测试包。您可以使用复选框选择要运行的测试,然后xcodebuild的测试操作将只运行这些测试。

+1

谢谢!实际上,我不需要为DYLD_FRAMEWORK_PATH和DYLD_LIBRARY_PATH设置环境变量。我需要确保在Build/Products/Testing中有正确的xctest bundle位置,而不是Build/Products/Debug。所以我的路径如下所示:./xctest -XCTest MyTestClass/testMyTestFunction /Users/username/Library/Developer/Xcode/DerivedData/MyApp-abcdefghikjklmn/Build/Products/Testing/MyAppTests.xctest –

+0

感谢关于禁用测试的提示方案。您可以选择多行并使用空格键切换选择 - 非常有用! – funroll