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:
-F...
adds XCTest.framework to the framework search paths, enabling it to be imported from Swift-Xlinker -rpath ...
makes sure the XCTest shared library can be found at load time- Without explicitly specifying
-lswiftCore
, I found thatxctest
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.
Press the "+" button, and select MacOS Unit testing Bundle
:
Name your testing bundle somehow, e.g. test
:
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 ):
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
Related Topics
Method' Is Ambiguous for Type Lookup in This Context, Error in Alamofire
Warning: Initialization of 'Unsafebufferpointer<T>' Results in a Dangling Buffer Pointer
How to Run Xctest for a Swift Application from the Command Line
What's the Equivalent of Finally in Swift
Storing Different Types of Value in Array in Swift
Swift 3: Atomic_Compare_Exchange_Strong
How to Use Combine to Track Uitextfield Changes in a Uiviewrepresentable Class
Getting Optional("") When Trying to Get Value from Keychain
Get Current Time as String Swift 3.0
Prevent Nsurlsession from Caching Responses
Exporting Mp4 Through Avassetexportsession Fails
Can't Create a Range in Swift 3
Swift 3 (Spritekit): Reseting the Gamescene After the Game Ends
In Swift, How to Get Memory Back to Normal After an Skscene Is Removed
Swift Error: Binary Operator '&&' Cannot Be Applied to Two 'Bool' Operands
Swift Closures Causing Strong Retain Cycle with Self
Get an Array of Dates of the Current Week Starting on Monday