Unit Test Your App
Create unit tests that automatically exercise the features of your application. Monitor the results of the tests and fix any issues from the test navigator.
You can use the Xcode service, available in OS X Server, to automate the execution of unit tests. From Xcode on your development Mac, you create bots that run on a separate server. In addition to running unit tests, bots automatically perform static analysis on your code, build your app, and archive it for distribution to testers or the App Store. Bots help you ensure that your product is always in a releasable state—and when there's a failure, the service notifies you or the person whose code change caused the failure.
Create and Run Unit Tests
Unit tests are programs that automatically exercise the features of your application and check the results. Unit tests are often used to detect regressions introduced by code changes to a project. Some developers write unit tests first, and then implement methods that pass the tests.
A unit of code is the smallest testable component of your project—for example, a method in a class or a set of methods that accomplish an essential purpose. A test case exercises a unit of code in a specific way; if the result of the test is different from the expected result, the test case fails. A test suite is made up of a set of test cases. You create test suites based on the XCTest framework. You can develop one or more test suites to test different aspects of your code.
When you create a project or a target, Xcode includes a unit test target in the scheme that builds the app. The implementation file for the target includes stubs for the
testExample methods. Complete these stub implementations and add other code as necessary to perform unit tests on your app.
Use the test navigator to run unit tests and review their status. You can add a test target to a project (or add a class to a test) by clicking the Add button (+) in the bottom-left corner of the test navigator. To view the source code for a particular test, select it from the test list. The file opens in the source code editor.
To run a test suite, click the arrow to the right of the name. To run a subset of test methods, select them in the test navigator and choose Product > Perform Action > Run Test methods. To run an individual test method, click the arrow to the right of the method name. Choose Product > Test to run all tests in the active scheme.
When a test succeeds, a green diamond with a checkmark denoting success appears to the right of the test name. When a test fails, a red diamond with an X denoting failure appears to the right of the test name, and the issue is displayed in the issue navigator, available by clicking in the navigator selector bar.
To view only the failed tests, click the Failed Test button () at the bottom of the test navigator. Select a failed method to examine it in the source code editor. After addressing the reason for the failure, click the failed test indicator (a red diamond with an X) to rerun the test.
From the jump bar menu for the assistant editor, you can use the Test Classes and Test Callers categories to place your tests and application code side by side in the workspace window.
Automate Unit Testing as Part of a Continuous Integration Workflow
Xcode supports a continuous integration workflow through the Xcode service. The Xcode service, available in OS X Server, automates the integration process of building, running unit tests, performing static analysis, and archiving your product. The service reports build errors and warnings, static analyzer problems, and unit test failures.
To run the Xcode service, install and configure OS X Server and Xcode on a Mac running OS X Mavericks. A continuous integration workflow typically relies on one or more development Mac computers running Xcode and on a separate server running the Xcode service. However, you can install OS X Server and run the Xcode service on your development Mac. Such a configuration can be helpful for evaluating how to adopt continuous integration. Afterward, you’ll find it more useful to have a dedicated server running the Xcode service, hosting your source code repositories, and performing integrations.
The Xcode service operates on projects contained in source code repositories. The Xcode service supports two popular source control systems: Git and Subversion. You can use Git and Subversion repositories hosted on remote servers. The Repositories tab in the Xcode service on the server allows you to connect to remote repositories.
Another valuable feature of the Xcode service is that you can use it to host Git repositories on your own server. Use the Repositories tab in the Xcode service on the server to configure access to its hosted repositories. On your development Mac, choose Preferences > Accounts, and add your credentials for the server and its repositories. When you create a project on your development Mac, you can simultaneously create a repository for it on the server.
If you already use a Git repository local to your development Mac, you can clone the repository to the server running the Xcode service. To clone a local repository to a server running the Xcode service, open the project on your development Mac, and choose Source Control > ProjectName > Configure ProjectName. Click Remotes, click the Add button (+), choose Create New Remote, and select the server.
Bots are at the heart of the automated workflow. Bots build and test products with the schemes of your choosing. You must share a scheme to enable a bot to access it. A shared scheme is one that you publish in a repository, along with the other shared project files. To share a scheme, choose Product > Scheme > Manage Schemes on your development Mac, and click the Shared option. Commit the change, and select the “Push to remote” option to publish the shared scheme in the repository.
You can create and schedule bots to run either periodically or on every source code commit. On your development Mac, open the project that contains the scheme that defines the actions to automate. Choose Product > Create Bot, and specify the attributes of the bot, including:
The integration schedule: periodically (hourly, daily, or weekly), on every commit, or manually
Who should receive email notifications from the bot when it succeeds and when it fails
For an iOS app, what kinds of devices or simulators the bot will test on
From the log navigator, available by clicking in the navigator selector bar on the development Mac, you can manage bots, view their test results, read integration logs, initiate integrations, and download product builds and archives. The Tests pane of the viewer provides a tabulated list of unit test results.
The Xcode service also hosts a bots website where you and members of your development team can perform these operations.