How do I run xctest from the command-line with Xco

2020-02-23 07:11发布

问题:

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

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

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

$ ./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:

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.

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 --test MyAppTests/testExample  /Users/myusername/Library/Developer/Xcode/DerivedData/MyApp-abcdefghijklmnop/Build/Products/Debug/MyAppTests.xctest

回答1:

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

xctest -XCTest MyAppTests/testExample testbundle.xctest

For a direct invocation of xctest to work you may also need to set DYLD_FRAMEWORK_PATH and DYLD_LIBRARY_PATH to 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 arguments and environment for [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.