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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19988723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:18:02  来源:igfitidea点击:

How do I run xctest from the command-line with Xcode 5?

xcodeunit-testingxctest

提问by Chris Livdahl

I found a command-line tool called "xctest" that apparently can run the unit tests in your project. This executable lives here:

我找到了一个名为“xctest”的命令行工具,它显然可以在您的项目中运行单元测试。这个可执行文件在这里:

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

When I try to run this executable on my xctest bundle, I'm using:

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

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

However, I get the following output:

但是,我得到以下输出:

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

There's no man page for xctest, as far as I can tell, but entering just ./xctest at the command-line yields:

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

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

In particular, I'd like to be able to test just a particular method in a test class, which is why I'd like to use this xctest command.

特别是,我希望能够仅测试测试类中的特定方法,这就是我想使用 xctest 命令的原因。

I do see that there is a way to run all the tests from the command line like:

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

$ xcodebuild test -scheme MyApp

This runs all the unit tests and works properly (I see my unit test results, unlike when using xctest). But I'm interested in being able to run a single test method from the command-line, such as:

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

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

采纳答案by Matt Stevens

Despite what the usage message says -XCTest is the argument you need:

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

xctest -XCTest MyAppTests/testExample testbundle.xctest

For a direct invocation of xctest to work you may also need to set DYLD_FRAMEWORK_PATHand DYLD_LIBRARY_PATHto your built products directory. In general you need to use the same arguments and environment as Xcode does, you can see this by putting a breakpoint in one of your tests, running them through Xcode, then printing out the values of argumentsand environmentfor [NSProcessInfo processInfo].

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

To avoid messing with all that note you can also modify the scheme in Xcode to run only specific tests. Under Product > Scheme > Edit Scheme select the Test action and expand the test bundle. You can use the check boxes to select the tests to run and xcodebuild's test action will then run only these tests.

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