Preparing XCUITest Tests for Upload

The steps necessary to prepare an app and its corresponding test suite for upload to App Center vary depending on the test framework. The section below provides instructions for preparing XCUITest tests for upload to App Center Test.

Requirements

  • Xcode >= 9.4.1
  • iOS >= 9.0

Build For Testing

To run a test in App Center, you need to build your application and an XCUITest bundle. To do this, run the following command from the root of your application project directory:

$ rm -rf DerivedData
$ xcrun xcodebuild build-for-testing \
  -configuration Debug \
  -workspace YOUR_WORKSPACE \
  -sdk iphoneos \
  -scheme YOUR_APP_SCHEME \
  -derivedDataPath DerivedData

This will build your app and an XCUITest bundle into the DerivedData/Build directory. Your app and XCUITest bundle will be located in the DerivedData/Build/Products/Debug-iphoneos/ directory.

YOUR_WORKSPACE should point to a .xcworkspace file, likely titled PROJECT_NAME.xcworkspace. YOUR_APP_SCHEME should be the scheme you use to build your application. By default, it is usually the name of your application. To see the list of schemes defined in your Xcode project, run:

$ xcrun xcodebuild -list

Uploading Tests to App Center

Once a test suite is prepared, next setup a test run to upload and run your tests.

Here is a sample script you might use to clean, build and submit your XCUITest tests.

# Generate an XCUITest bundle and your iOS application as described above.
$ rm -rf DerivedData
$ xcrun xcodebuild build-for-testing -derivedDataPath DerivedData -scheme YOUR_APP_SCHEME

# Upload your test to App Center
$ appcenter test run xcuitest \
  --app "<app center username/<app name>" \
  --devices DEVICE_SET \
  --test-series "master" \
  --locale "en_US" \
  --build-dir DerivedData/Build/Products/Debug-iphoneos

For a concrete example of submitting tests to App Center, see this shell script that submits tests to App Center.

Additional Screenshots and Test Steps

At the end of each test method, a label and screenshot are automatically generated for the test report. Labels and screenshots are displayed in the App Center Test Report as separate Test Steps.

Activities

You can generate additional labels and screenshots for a test method by grouping your test code using Apple's Activities. For more information, see Grouping Tests into Substeps with Activities.

Code snippet to wrap test code in an Activity

    [XCTContext
     runActivityNamed:title
     block:^(id<XCTActivity>  _Nonnull activity) {
         // test code
     }];

        XCTContext.runActivity(named: title) { (activity) in
            // test code
        }

Manual Screenshots

A screenshot is automatically generated for the test report at the end of each Activity. You can record a screenshot at a different point within an Activity by taking a screenshot using XCUIScreen, then creating an attachment with XCTAttachment, and adding it to the current Activity. This screenshot will be displayed in App Center Test Reports instead of the default screenshot from the end of the Activity.

Code snippet to generate and attach a screenshot in an Activity

         XCUIScreenshot *screenshot = [[XCUIScreen mainScreen] screenshot];
         XCTAttachment *attachment;
         attachment = [XCTAttachment attachmentWithScreenshot:screenshot];
         [attachment setLifetime:XCTAttachmentLifetimeKeepAlways];
         [activity addAttachment:attachment];
            let screenshot = XCUIScreen.main.screenshot()
            let attachment = XCTAttachment(screenshot: screenshot)
            attachment.lifetime = .keepAlways
            activity.add(attachment)