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

14,737

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.

Share:
14,737

Related videos on Youtube

Chris Livdahl
Author by

Chris Livdahl

I finished attending school to finish up a Master of Science in Computer Science and Software Engineering, as well as an iOS/Mac Development Certificate. I'm currently a Lead iOS and Full-Stack Developer at Ratio/Globant.

Updated on June 12, 2022

Comments

  • Chris Livdahl
    Chris Livdahl almost 2 years

    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
    
  • Chris Livdahl
    Chris Livdahl almost 10 years
    Thanks! I actually did not need to set the environment variables for DYLD_FRAMEWORK_PATH and DYLD_LIBRARY_PATH. I needed to make sure I had the correct xctest bundle location in Build/Products/Testing, instead of Build/Products/Debug. So my path was like: ./xctest -XCTest MyTestClass/testMyTestFunction /Users/username/Library/Developer/Xcode/DerivedData/MyApp-ab‌​cdefghikjklmn/Build/‌​Products/Testing/MyA‌​ppTests.xctest
  • funroll
    funroll almost 10 years
    Thanks for the tip about disabling tests in the scheme. You can select multiple rows and use spacebar to toggle selection--very useful!