What’s New in Xcode 8.3 for XCTest and Xcode Server

Apple has just released Xcode 8.3 beta for the developers. Xcode 8.3 release notes has handful of the features for the developers. Some of the key features of Xcode 8.3 are

  • Autocompletion in the breakpoint navigator text fields.
  • XCTest has couple of  new classes to support asynchronous testing.
  • New user Interface of managing and signing certificate and provisioning profiles.
  • Deprecated Swift 2.3 and UI Automation completely.
  • Command line interface for Simulator simctl has two new options for logging
  • Changes in xcconfig doesn’t need Xcode restart.
  • Various bug fixes for xcodebuild and Swift Package Manager.

There are few enhancements on Swift Package Manager like running the XCTests in parallel but they seems to have some issues. In this post, we will see what is new in the Xcode 8.3 for XCTest and other developer tools.

Xcode 8.3

Xcode 8.3 is currently available for the download if you have Apple Developer Account. You can get it from the Downloads section of the developer account. Xcode 8.3 needs macOS version 10.12 and above. You can download compressed XIP file which is around 4.52 GB.  If you already have previous version of the Xcode then remove it or you can keep it but you have to switch between Xcode DEVLOPER_DIR. Once downloaded you can extract the file to install Xcode 8.3 beta and wait for installation of Xcode and command line tools.

Once Xcode 8.3 is fully installed with all the command line tools, we can drag it into /Applications path. Now, we have to switch to the new Xcode version by running following command

This will set new DEVELOPER_DIR and  we are ready to use Xcode 8.3.

Xcode 8.3 and XCTest

The XCTest framework allows developers to write unit and UI tests for iOS, macOS apps. Apple introduced Xcode UI testing in the WWDC 2015 which allow us to write UI tests within the Xcode. As part of the Xcode 8.3 release, Apple has aded couple of new class to the XCTest framework to support asynchronous testing. It means there are no handlers involved while waiting for the XCUIElement  to appear. Previously, we had waitForExpectations(timeout: handler:) method combined with XCTestExpectation  to test asynchronous code which looks like this:

This piece of code will wait for 3 seconds to find the button and it will fail after 3 second if it doesn’t find element. The XCTest Framework now has XCTWaiter class to wait for the element. The list of the newly added classes and sub-classes  are as follows

In order to explore, these newly added classes, I have created a sample project on Github Xcode83_Demo which is app with a button and textField.

XCTestWaiter

XCWaiter class returns results of the expectations in the form of boolean. It returns enum of four possible situations those are .completed , .timedOut , .incorrectOrder  or .invertedFulFilment . I have given a try for the completed and timedOut. We can simply add extension to XCUIElement  or add simple function which returns result of waiter function.

Now that,  we can check the value of the result in out test like this:

XCTWaiter class gives us control over failure condition of the tests rather than just fails to throws exception after timeout. There are some XCUI tests in the demo repo, you can find here.

Readable Expectations

There are some new expectations added to the XCTest Framework which are XCTNSPredicateExpectationXCTKVOExpectation and XCTDarwinNotificationExpectation. I think, idea behind that is to make expectations more readable and customisable. Previously we have to wtite expectations with handlers. Now we can write expectation like this :

We can then pass this expectation to the XCWaiter to get boolean result. You can find some examples in the demo repo here.

XCTestExpectation Bug Fix

As per the release note, there was a bug in XCTestExpectations which allowed us to carry on with execution even with incorrect XCUIElement  query. Now those buggy tests will start failing as part of this fix .

Parallel Running of XCTest with Swift Package Manager

There is a additional flat to run XCTests in the parallel for Swift Package Manager. Swift Packages now should have swift package test --parallel   option to run the tests in parallel but there are some known issues with it. I couldn’t see that options for my Swift packages somehow.

Xcode 8.3 and Xcode Server

Xcode 8.3 required macOS version 5.2 and above to perform continuous integration with Xcode Server. However the process of setting up the macOS server hasn’t changed. In my previous post, I have covered complete step by step step of Xcode Server and Xcode Bots. Here is how I have setup macOS Server for the Xcode Service using Xcode 8.3.

There is nothing much changed in Xcode Server setup.

Conclusion

Xcode 8.3 release has lot of feature related to XCTest which mean Apple is investing in the testing tools and practices which is good sign for all of us. Look forward to see more features and enhancements in XCTest.