How to Run Xctest for a Swift Application from the Command Line

How can I run XCTest for a swift application from the command line?

I was able to get your XCTestCase compiling with the following command:

swiftc \
-F/Applications/Xcode6-Beta5.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks \
-Xlinker -rpath -Xlinker /Applications/Xcode6-Beta5.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks \
-lswiftCore LeapTest.swift \
-o LeapTests

Then you can execute the tests with xctest:

xcrun xctest LeapTests

And to break down those swiftc command line options:

  1. -F... adds XCTest.framework to the framework search paths, enabling it to be imported from Swift
  2. -Xlinker -rpath ... makes sure the XCTest shared library can be found at load time
  3. Without explicitly specifying -lswiftCore, I found that xctest would crash when it tried to run the test suite

Hope that helps!

XCTest for a Mac command line application

So, you have a simple command line app with main.swift/main.m and maybe some other code.

By default you have only one target with the same name as your product.

Press the "project" file, and you'll get smth like this:

The blue icon in the left-top corner is your project's file, and in the right-bottom part you have a list of targets.

Sample Image

Press the "+" button, and select MacOS Unit testing Bundle:
Sample Image

Name your testing bundle somehow, e.g. test:
Sample Image

Now your project contains 2 targets: one "main" (named the same as the project) and one "testing".

When adding a new file, don't forget to add it to the testing target (the checkbox under the Targets section ):
Sample Image


Please note that this type of testing target is Logic tests, not Application tests, for more info look here: https://forums.developer.apple.com/thread/52211

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

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.

Running individual XCTest (UI, Unit) test cases for iOS apps from the command line

It is now possible with Xcode 8 using the -only-testing parameter with xcodebuild:

xcodebuild test -workspace <path>
-scheme <name>
-destination <specifier>
-only-testing:TestBundle/TestSuite/TestCase

Sample Image



Related Topics



Leave a reply



Submit