Unbounded Thoughts

Dec04

XCode 5 – XCTest Unit Testing Framework for iOS Developers

In a recent blog post we talked about how iOS Developers can enhance application development for iPhones and iPad by doing some performance tuning in Core Data.  Now we want to look at some of the other enhancements developers can utilize as they develop apps for iOS 7.

While much of the Apple mobile community is still agog over the significant changes to the look and feel of the operating system with the advent of the flat design concept, developers know that the real magic is happening under the hood.  There are a host of improvements to the developer’s toolset, and we want to take a closer look at a key improvement found in XCode 5 – The Unit Testing Framework.

Test Navigator

XCode 5 is intended to make the job of an iOS developer a little bit easier.  This isn’t to say that you can just bang out some code and call it a day, but rather that it makes it a heck of a lot easier to test units of code, and isolate problems in your code.  In fact, Apple really pushes the concept of ‘test-driven development’ as a best practice.  So let’s take a look at how to use the Test Navigator.

The Test Navigator allows the iOS Development Team to run a individual test, a subset of a specific  test, or you can go hog-wild and run a suite of tests with a few clicks of the mouse.

To run a single test, click the arrow next to the method name and then select Product > Test.  To run a test on a subset of test methods, simply select them in the test navigator and then choose Product > Perform Action > Run Test Methods.

Assistant Editor

Let’s say that you’ve found that you have a problem with your code – you’ll know it by the culprit being highlighted in red – but still…  What do you do to fix it?  The Assistant Editor can help you here.

With the Assistant Editor, an iOS developer can use the Test Classes and Test Callers categories to easily check the error in the given test case with code and tests placed side by side in the workspace window.

xcode and unit testing

A Look at the Xcode Unit Test Interface – Click the Image for a Larger View

XCTest

XCTest replaces the SenTestingKit and is included by default with the iOS 7 development kit.  In many ways is seems that this is more of a rename vs a rebuild, as assertions being renamed from STxxxx to XCTxxxx, but it does seem to offer a better environment with the new continuous integration functionality.

XCTest enables iOS app developers to write tests that run on different Mac and iOS devices (like the iPhone, iPad Air and iPad mini) and simulate them from within XCode or from the command line.

Assertions Supported by XCTest

Short list of assertions supported by XCTest:

  • XCTFail (format…)
  • XCTAssertNil (a1, format…)
  • XCTAssertNotNil (a1, format…)
  • XCTAssert (a1, format…)
  • XCTAssertTrue (a1, format…)
  • XCTAssertFalse (a1, format…)
  • XCTAssertEqualObjects (a1, a2, format…)
  • XCTAssertEquals (a1, a2, format…)
  • XCTAssertEqualsWithAccuracy (a1, a2, accuracy, format…)
  • XCTAssertThrows (expression, format…)
  • XCTAssertThrowsSpecific (expression, specificException, format…)
  • XCTAssertThrowsSpecificNamed (expression, specificException, exceptionName, format…)
  • XCTAssertNoThrow (expression, format…)
  • XCTAssertNoThrowSpecific (expression, specificException, format…)
  • XCTAssertNoThrowSpecificNamed (expression, specificExcepton, exceptionName, format…)

If you’re not sure what each of these assertions actually do, today is your lucky day!  All you need to do to get a definition of an assertion is to Ctrl+Click on the assertion (Like “XTCAssertNoThrow”) and then click on “Jump to Definition”.

We hope that this helps you to get the most out of the XCTest in XCode 5.  If so, do us a solid and share this post across your social networks.  If not, tell us where you’re running into problems with the comment section below.

2 Comments

  1. […] test the app on the targeted devices.  Normally, the iOS developer or dev team would do this by using XCode 5, but what if you don’t have access to […]

  2. […] Unit testing, quality assurance, user acceptance testing, and the like are essential to guaranteeing a successful and well received app. A well received app is the ultimate goal as this will lead to future requests and the culmination of a successful project/venture. […]

Leave a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>