How to unit test an app extension on Xcode 6
I have reported the bug to Apple. And sadly, the answer is that the keyboard extension is not support unit test now. The answer comes from Apple:
- It's not currently supported to run unit tests inside an app extension
- Instead, factor the code you want to test into a framework and test the code there
- Link the framework into your extension
Testing iOS Keyboard App Extension
After asking to Apple Developpers, the answer is no.
There is no way to do UITesting for Keyboard Extensions for now.
Writing a unit-test for a class extension in Swift
As @dan alluded to, you need to call it from an instance of the UIViewController
. Usually you don't want to instantiate framework objects in your tests if you can avoid it, so here are some options to avoid that:
- Make
presentAlert
static so that you can justUIViewController.presentAlert
- Make
presentAlert
a free function (don't put it in the extension) - Extend a protocol instead – I think this is the cleanest option
protocol Presentable {}
extension Presentable {
func presentAlert(title: String, message : String) { /* ... */ }
}
Then, whenever you need it you can extension UIViewController: Presentable {}
. And in your tests, you can just use a dummy class. The added benefit with this approach is that you can reuse that function on any type if needed, without globally exposing it when you don't.
Addendum
When we extend the protocol we are saying "anything that implements this protocol will get this method for free." The trick here is that this protocol is empty and, therefore, very easy to "implement."
extension YourViewController: Presentable {}
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 Change iPhone Uitableview Delete Button Title While Editing It
App Is Not Showing in the Share Menu of Shared Options in Shared Extension in iOS8
Nspredicate Filtered by Year Moth Day
Sort Array by Distance Near User Location from Firebase
Programmatically Sending an App to Background
Odd Property Declaration Syntax Containing Angular Brackets <>
Regex to Get String Between Two % Characters
Detox: iOS Simulator How to Confirm Alert Message
Not Working Transitions in Spritekit
Xcode 9 Crash When Open Storyboard
Using Two Versions of a Cocoapod Dependency
iOS Notification When Application Is Closed
How to Upload Multiple Image on Firebase Using Swift
Collectionview Duplicate Cell When Loading More Data
How to Remove Black Edge on Uiimageview with Rounded Corners and a Border Width
How to Change Uibutton Image After Clicking in Swift
Animation Blocks Resets to Original Position After Updating Text