Skip to main content
Transform your testing process with: Real Device Features, Company-wide Licences, & App Percy
No Result Found
Get your setup working faster. Join our Discord for optimisation tips from elite testers. Join our DiscordJoin our Discord

Speed up XCUI tests

Speed up your XCUI mobile automation tests on BrowserStack App Automate. Run tests on real device cloud.

Parallel testing enables you to reduce the overall test execution times, resulting in improved build times and faster releases. Test sharding enables you to test at scale on BrowserStack.

What is test sharding

By default, all test cases in your XCUI test suite are executed sequentially. Instead of running your test suite sequentially, you can split test cases into different groups called shards. Each shard can then be executed in parallel on one or more devices. This is especially helpful when your test suite has a large number of test cases.

For example, if your test suite has 500 test cases and each test case takes about a minute to execute, running the test suite sequentially takes close to 500 minutes. If you split your test suite into 10 shards and execute each shard in parallel, the total test execution time comes down to 50 minutes.

Enable test sharding

To enable test sharding, configure the shards parameter in the REST API request to start XCUI test execution.

REST API endpoint:

POST /app-automate/xcuitest/v2/build
Parameter Description Values
shards Split the test-suite into multiple shards and execute them in parallel. An object (key-value pairs) to specify the number of shards and configure its behavior. Refer to REST API for detailed reference.

Example cURL request:

curl -u "YOUR_USERNAME:YOUR_ACCESS_KEY" \
-X POST "https://api-cloud.browserstack.com/app-automate/xcuitest/v2/build" \
-d '{"shards": {"numberOfShards": 2, "mapping": [{"name": "Shard 1", "strategy": "only-testing", "values": ["SampleXCUITests/testAlert", "SampleXCUITests/testText"]}, {"name": "Shard 2", "strategy": "only-testing", "values": ["SampleXCUITests/testLogin"]}]}, "devices": ["iPhone X-11"], "app": "<app_url>", "testSuite": "<test_suite_url>"}' \
-H "Content-Type: application/json"

In this example, the test suite is split into 2 shards, and each of those shards executes in parallel on iPhone X-11. This blocks 2 parallel test licenses in your App Automate subscription plan.

Sharding strategy

There are multiple strategies to control how you want to split the test suite into different shards.

1. Use only-testing strategy

Using this strategy, you can group test cases by specifying the classes or tests (e.g. SampleXCUITestsClass/testAlert) in different shards. All the shards execute in parallel on the devices specified in the devices parameter.

REST API endpoint:

POST /app-automate/xcuitest/v2/build

Example cURL request:

curl -u "YOUR_USERNAME:YOUR_ACCESS_KEY" \
-X POST "https://api-cloud.browserstack.com/app-automate/xcuitest/v2/build" \
-d '{"shards": {"numberOfShards": 2, "mapping": [{"name": "Shard 1", "strategy": "only-testing", "values": ["SampleXCUITestsClass/testAlert", "SampleXCUITestsClass/testText"]}, {"name": "Shard 2", "strategy": "only-testing", "values": ["SampleXCUITestsClass/testLogin"]}]}, "devices": ["iPhone X-11","iPhone 8 Plus-11"], "app": "<app_url>", "testSuite": "<test_suite_url>"}' \
-H "Content-Type: application/json"

In the above example, the test cases are split into 2 groups called shards. The first shard consists of test cases testAlert and testText of class SampleXCUITestsClass. The second shard consists of test case testLogin of class SampleXCUITestsClass. Both the shards execute in parallel on iPhone X-11 and iPhone 8 Plus-11. This blocks 4 parallel test licenses in your App Automate subscription plan.

2. Use skip-testing strategy

Using this strategy, you can group test cases by skipping classes or tests (e.g. SampleXCUITestsClass/testAlert) in different shards. All the shards execute in parallel on the devices specified in the devices parameter.

REST API endpoint:

POST /app-automate/xcuitest/v2/build

Example cURL request:

curl -u "YOUR_USERNAME:YOUR_ACCESS_KEY" \
-X POST "https://api-cloud.browserstack.com/app-automate/xcuitest/v2/build" \
-d '{"shards": {"numberOfShards": 2, "mapping": [{"name": "Shard 1", "strategy": "skip-testing", "values": ["SampleXCUITestsClass/testAlert"]}, {"name": "Shard 2", "strategy": "skip-testing", "values": ["SampleXCUITestsClass/testText"]}]}, "devices": ["iPhone X-11","iPhone 8 Plus-11"], "app": "<app_url>", "testSuite": "<test_suite_url>"}' \
-H "Content-Type: application/json"

In the above example, the test cases are split into 2 groups called shards. The first shard consists of all test cases of class SampleXCUITestsClass except testAlert. The second shard consists of all test cases of class SampleXCUITestsClass except testText. Both the shards execute in parallel on iPhone X-11 and iPhone 8 Plus-11. This blocks 4 parallel test licenses in your App Automate subscription plan.

Device selection strategy

Use the deviceSelection attribute inside the shards parameter to control which devices each shard runs on. If you don’t set it, BrowserStack uses all.

Parameter Description Values
deviceSelection Run each shard on all devices, on any randomly selected device from the list of devices specified in the devices parameter, or on one device that you choose for each shard. all, any, oneToOne
Default: all

all: Each shard runs on all devices specified. Thus, the total number of test sessions in the build equals (n shards * m devices).
any: Each shard runs on any one randomly selected device. Thus, the total number of test sessions in the build equals (n shards).
oneToOne: Each shard runs on exactly one device, the device set in the shard’s mapping entry. If a shard has no device, it runs on the device at the shard’s position in the devices parameter. Thus, the total number of test sessions in the build equals (n shards).

The value you choose also decides how many parallel test licenses the build blocks. For a build with x shards and y devices:

  • all: Each shard runs on every device in the devices parameter. The build blocks x × y parallel test licenses.
  • any: Each shard runs on one device randomly selected from the devices parameter. The build blocks x parallel test licenses.
  • oneToOne: Each shard runs on one device that you choose. The build blocks x parallel test licenses.

Run each shard on all devices

Use all when every shard must run on every device in the devices parameter, for example, to confirm that your whole test suite passes on each device model. all is the default value, so each shard runs on all devices even if you don’t set deviceSelection.

REST API endpoint:

POST /app-automate/xcuitest/v2/build

Example cURL request:

curl -u "YOUR_USERNAME:YOUR_ACCESS_KEY" \
-X POST "https://api-cloud.browserstack.com/app-automate/xcuitest/v2/build" \
-d '{"shards": {"numberOfShards": 2, "deviceSelection": "all", "mapping": [{"name": "Shard 1", "strategy": "only-testing", "values": ["SampleXCUITests/testAlert", "SampleXCUITests/testText"]}, {"name": "Shard 2", "strategy": "only-testing", "values": ["SampleXCUITests/testLogin"]}]}, "devices": ["iPhone X-11", "iPhone 8 Plus-11"], "app": "<app_url>", "testSuite": "<test_suite_url>"}' \
-H "Content-Type: application/json"

In the above example, the test cases are split into 2 shards. Each shard runs on both iPhone X-11 and iPhone 8 Plus-11, so the build runs 4 test sessions. This blocks 4 parallel test licenses in your App Automate subscription plan.

Run each shard on a random device

Use any when you want to run the test suite with fewer parallel test licenses and it doesn’t matter which device runs each shard. Each shard runs on one device randomly selected from the devices parameter, so a shard can run on a different device in each build.

REST API endpoint:

POST /app-automate/xcuitest/v2/build

Example cURL request:

curl -u "YOUR_USERNAME:YOUR_ACCESS_KEY" \
-X POST "https://api-cloud.browserstack.com/app-automate/xcuitest/v2/build" \
-d '{"shards": {"numberOfShards": 2, "deviceSelection": "any", "mapping": [{"name": "Shard 1", "strategy": "only-testing", "values": ["SampleXCUITests/testAlert", "SampleXCUITests/testText"]}, {"name": "Shard 2", "strategy": "only-testing", "values": ["SampleXCUITests/testLogin"]}]}, "devices": ["iPhone X-11", "iPhone 8 Plus-11"], "app": "<app_url>", "testSuite": "<test_suite_url>"}' \
-H "Content-Type: application/json"

In the above example, the test cases are split into 2 shards. Both the shards execute in parallel, but each shard executes on a single device randomly selected from the list, either iPhone X-11 or iPhone 8 Plus-11. This blocks 2 parallel test licenses in your App Automate subscription plan, one for each shard.

Run each shard on a device you choose

Use oneToOne when each shard must run on a specific device, for example, to run one group of tests on a newer device and another group on an older device. Each shard runs on exactly one device:

  • If the shard’s mapping entry has a device, the shard runs on that device.
  • If the shard has no device, it runs on the device at the shard’s position in the devices parameter: the first shard on the first device, the second shard on the second device, and so on. If there are more shards than devices, the assignment wraps around to the first device. The position counts every shard, including shards that set their own device. For example, if you have three shards and only the first shard sets a device, the second shard runs on the second entry in the devices parameter, not the first.

If every shard has its own device, you can omit the devices parameter. The build blocks one parallel test license for each shard.

Set a device for each shard

Add a device to each shard’s mapping entry. This works the same way with the only-testing and skip-testing strategies.

REST API endpoint:

POST /app-automate/xcuitest/v2/build

Example cURL request:

curl -u "YOUR_USERNAME:YOUR_ACCESS_KEY" \
-X POST "https://api-cloud.browserstack.com/app-automate/xcuitest/v2/build" \
-d '{"shards": {"numberOfShards": 2, "deviceSelection": "oneToOne", "mapping": [{"name": "Shard 1", "strategy": "only-testing", "values": ["SampleXCUITests/LoginTests"], "device": "iPhone 15-17"}, {"name": "Shard 2", "strategy": "only-testing", "values": ["SampleXCUITests/PaymentTests"], "device": "iPhone 14-16"}]}, "app": "<app_url>", "testSuite": "<test_suite_url>"}' \
-H "Content-Type: application/json"

In the above example, the first shard runs only on iPhone 15-17, and the second shard runs only on iPhone 14-16. The request omits the devices parameter because every shard has its own device. This blocks 2 parallel test licenses in your App Automate subscription plan, one for each shard.

Combine shard devices with the devices parameter

You can set a device for some shards and let the others take a device from the devices parameter.

REST API endpoint:

POST /app-automate/xcuitest/v2/build

Example cURL request:

curl -u "YOUR_USERNAME:YOUR_ACCESS_KEY" \
-X POST "https://api-cloud.browserstack.com/app-automate/xcuitest/v2/build" \
-d '{"shards": {"numberOfShards": 2, "deviceSelection": "oneToOne", "mapping": [{"name": "Shard 1", "strategy": "only-testing", "values": ["SampleXCUITests/LoginTests"], "device": "iPhone 15-17"}, {"name": "Shard 2", "strategy": "only-testing", "values": ["SampleXCUITests/PaymentTests"]}]}, "devices": ["iPhone 14-16"], "app": "<app_url>", "testSuite": "<test_suite_url>"}' \
-H "Content-Type: application/json"

In the above example, the first shard runs on iPhone 15-17, its own device. The second shard has no device, so it takes the device at its position in the devices parameter. The list has only one device, so the assignment wraps around and the second shard runs on iPhone 14-16.

Select a device using a regex pattern

Instead of an exact device, you can set a shard’s device to an object with device and optional os_version regex patterns. The shard runs on any available device that matches.

REST API endpoint:

POST /app-automate/xcuitest/v2/build

Example cURL request:

curl -u "YOUR_USERNAME:YOUR_ACCESS_KEY" \
-X POST "https://api-cloud.browserstack.com/app-automate/xcuitest/v2/build" \
-d '{"shards": {"numberOfShards": 2, "deviceSelection": "oneToOne", "mapping": [{"name": "Shard 1", "strategy": "only-testing", "values": ["SampleXCUITests/LoginTests"], "device": {"device": "iPhone 15.*"}}, {"name": "Shard 2", "strategy": "only-testing", "values": ["SampleXCUITests/PaymentTests"], "device": {"device": "iPhone 14.*", "os_version": "1[6-8]"}}]}, "app": "<app_url>", "testSuite": "<test_suite_url>"}' \
-H "Content-Type: application/json"

In the above example, the first shard runs on any available iPhone 15 model, and the second shard runs on any available iPhone 14 model with iOS 16, 17, or 18.

Rules for the oneToOne value

Follow these rules when you use oneToOne:

  • The device in a shard’s mapping entry applies only when deviceSelection is set to oneToOne. With all or any, it’s ignored.
  • Use one format throughout. The devices parameter and every device in mapping must all be exact Device-OS strings, or must all be regex objects.
  • A regex object must contain a pattern, such as .* or [34]. To run a shard on a fixed device, use the exact Device-OS string instead of an object.
  • Every shard needs a device, either its own device in mapping or an entry in the devices parameter to fall back on.

If your request doesn’t follow these rules, the API returns one of the following errors and doesn’t create the build:

Error message Cause Fix
[BROWSERSTACK_SHARDS] With "deviceSelection" set to "oneToOne", every shard needs a device: either a "device" in its mapping or an entry in the "devices" parameter. Please pass the required parameters in a valid format and try again. Some shards set a device and others don’t, and the devices parameter is missing or empty. Add a device to every shard’s mapping entry, or pass at least one device in the devices parameter.
[BROWSERSTACK_SHARDS] The device defined in the mapping for a shard must use the same format as the entries of the "devices" parameter. Please pass the required parameters in a valid format and try again. The devices parameter and the device values in mapping mix exact Device-OS strings and regex objects, a regex object has no device key, or a device value is a number, array, or boolean. Use one format throughout: either exact Device-OS strings or regex objects for the devices parameter and every device in mapping.
[BROWSERSTACK_INVALID_PARAMETER_VALUE_ERROR] Invalid value passed for devices parameter. Please pass a valid value and try again. ... A device in mapping is a regex object with no regex pattern, such as {"device": "iPhone 15"}. Use a regex pattern such as .*, or the exact Device-OS string.
[BROWSERSTACK_INVALID_DEVICE] ... A device in mapping uses the correct format but names a device that is invalid or unavailable. Use a device from the list of supported devices.

Sharding with XCTest Plans

If you run iOS tests using Xcode Test Plans (.xctestrun flow), you can use the same shards parameter to split and parallelize test execution. The sharding contract is identical to the default XCUITest flow. It uses the same numberOfShards, mapping, strategy, and deviceSelection attributes.

REST API endpoint:

POST /app-automate/xcuitest/v2/xctestrun-build

Example cURL request:

curl -u "YOUR_USERNAME:YOUR_ACCESS_KEY" \
-X POST "https://api-cloud.browserstack.com/app-automate/xcuitest/v2/xctestrun-build" \
-d '{"shards": {"numberOfShards": 3, "mapping": [{"name": "Shard 1", "strategy": "only-testing", "values": ["MyUITests/testA", "MyUITests/testB"]}, {"name": "Shard 2", "strategy": "only-testing", "values": ["MyUITests/testC"]}, {"name": "Shard 3", "strategy": "skip-testing", "values": ["MyUITests/testFlakyOnCI"]}]}, "devices": ["iPhone 14-16"], "app": "<app_url>", "testSuite": "<test_suite_url>"}' \
-H "Content-Type: application/json"

The only-testing and skip-testing strategies work the same way as in the default XCUITest flow. The all and any values of the deviceSelection attribute also behave identically. For more information, see Device selection strategy.

If you use singleRunnerInvocation alongside sharding, it applies within each shard session, running all tests in that shard in one process.

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback!

Talk to an Expert
Download Copy Check Circle