XCTest vs KIF Comparison (Medium.com Link)

This very interesting blog post by Laurin is worth studying, he showed how to use the new UI Testing provided by Xcode 7 !

Along with examples, it also has a performance comparison between KIF and XCTest for UI, with XCTest coming out slightly faster than KIF and with animations disabled, much faster. However it found that advanced tests with XCTest are still limited and so can not replace KIF yet. 

UI Testing with Xcode 7  

UI Testing is built into XCTest – on top a new version UIAutomation – more stable and reliable. Has: UI recording, automatic screenshots..

First test:

We have a controller, is the controller dismissed when tapping cancel button?

func testDismissal ()
{
    let imageController = ImagePickerSheetController()
    imageController.addAction(ImageAction(title: “Cancel”))
    rootViewController.presentViewController(imageController, animated: true, completion: nil)
    tester().tapViewWithAccessibilityLabel(“Cancel”)    
    tester().waitForAbsenceOfViewWithAccessibilityIdentifier(ID)
}

Second test:

for Crimson keyboard, when AccentView appearing, ‘e’ should be selected. Accent view should have same color as key?

To test content of a view – we can use XCUIElement property: value, this return the accessibilityValue of underlying UI element -> the AccentView:

class AccentView: UIView {
    var selectedLetter: String {
       didSet {
            accessibilityValue = selectedLetter
        }
    }
}

// This way we can check the selectedLetter of the Accent View. However the accessibilityValue is String -> cannot expose the color -> second test is not possible.

Benchmark

The result of comparing XCTest with KIF, better not relaunch for every spec (which is recommended by Apple, but should not do so)!

Normally XCTest is faster than KIF (not relaunch), but if we turn off animation, it’s nearly 4 times quicker than KIF.

So, we can set the Animations by environment:

class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 
    {
        if NSProcessInfo.processInfo().environment[“animations”] == “0”    {
            UIView.setAnimationsEnabled(false)
        }    
        ...
        return true
    }
}

Summary:

XCTest is good for simple UI Tests. But KIF is more flexible.

However, XCTest is slightly faster at finding UI elements -> if animation disabled -> XCTest is really fast.

Advertisements