Is it possible to run XCTest tests in an iOS app?
It is possible! The key is to get a copy of the XCTest framework linking and then use the public XCTest APIs to run your tests. This will give you the same console output you see when running the tests via Xcode. (Wiring up your own, custom reporter looks doable using the public APIs, but asking how to do so would make a good question in itself - not many people use the XCTest APIs, because Xcode does the dirty work for us.)
Copy In XCTest.framework
Rather than unravel whatever is breaking the module loading, you can copy the XCTest.framework bundle into your project from the platform frameworks for your target platform:
mkdir Frameworks && mkdir Frameworks/iphonesimulator
PLATFORMS=/Applications/Xcode.app/Contents/Developer/Platforms
FRAMEWORKS=Developer/Library/Frameworks
cp -Rp \
"$PLATFORMS/iPhoneSimulator.platform/$FRAMEWORKS/XCTest.framework" \
Frameworks/iphonesimulator/
Link Against Your Own XCTest
Then, pop open the target editor for your main app target, and drag and drop your copy from Finder to the list of "Linked Frameworks and Libraries".
Build Your Tests Into the Main App Target
Now, go to your test files, pop open the File Inspector, and tick the box next to your main app target for those files. Now you're building the test files as part of your main app, which puts all your XCTestCase
subclasses into the binary.
Run Those Tests
Lastly, wire up a button to "Run Tests" to an action like this:
import UIKit
import XCTest
class ViewController: UIViewController {
@IBAction func runTestsAction() {
print("running tests!")
let suite = XCTestSuite.default()
for test in suite.tests {
test.run()
}
}
}
When you tap the button, you'll see this in the console:
running tests!
Test Suite 'RunTestsInApp.app' started at 2017-05-15 11:42:57.823
Test Suite 'RunTestsInAppTests' started at 2017-05-15 11:42:57.825
Test Case '-[RunTestsInApp.RunTestsInAppTests testExample]' started.
2017-05-15 11:42:57.825 RunTestsInApp[2956:8530580] testExample()
Test Case '-[RunTestsInApp.RunTestsInAppTests testExample]' passed (0.001 seconds).
Test Case '-[RunTestsInApp.RunTestsInAppTests testPerformanceExample]' started.
/Users/jeremy/Workpad/RunTestsInApp/RunTestsInAppTests/RunTestsInAppTests.swift:34: Test Case '-[RunTestsInApp.RunTestsInAppTests testPerformanceExample]' measured [Time, seconds] average: 0.000, relative standard deviation: 122.966%, values: [0.000002, 0.000001, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100
Test Case '-[RunTestsInApp.RunTestsInAppTests testPerformanceExample]' passed (0.255 seconds).
Test Suite 'RunTestsInAppTests' passed at 2017-05-15 11:42:58.081.
Executed 2 tests, with 0 failures (0 unexpected) in 0.256 (0.257) seconds
Test Suite 'RunTestsInApp.app' passed at 2017-05-15 11:42:58.081.
Executed 2 tests, with 0 failures (0 unexpected) in 0.256 (0.258) seconds
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
How to take iOS Framework testing target and run it in a dummy app?
Ah, this article helped:
https://medium.com/@ryuichi/setup-host-app-for-keychain-tests-in-xcode-8-97222611917e#.z3zpqwnzt
I was adding a totally new app project, I just needed to add a new Target in the Framework project for a Single View Application
That then allows me to select Host Application
in the testing target.
Using my previous diagram, the final result that worked is:
Framework Project
|
|- Unit Test Target
|- Framework Target
|- Dummy App Target
Related Topics
How to Reset the Application Data After Each Test with Xcode 7 UI Testing
iOS 10 Heading Arrow for Mkuserlocation Dot
Swift Add Footer View in Uitableview
How to Take Screenshot of Portion of Uiview
Scanning Barcode or Qr Code in Swift 3.0 Using Avfoundation
How to Determine If the Sim/Phone Number Has Changed
Workaround for Rounded Corners of Grouped Uitableview, iOS7
Skspritenode Position in Universal Game
How to Create Custom View Programmatically in Swift Having Controls Text Field, Button etc
Where to Place a .Txt File and Read from It in a iOS Project
How to Retrieve Messages Logged with Os_Log from iPad/Iphone
Trouble Creating Xmpp Muc Room: Code 503 (Service Unavailable)
Uitextfield Not Getting Keyboard Input
Can't Set Background Color of Uitableviewcell in Ib
How to Customize Uirefreshcontrol with Different Image and Position
While Mail Sending Process Using iOS 8 Simulators Generates Error