# `apple_test_bundle`

Apple test bundle.

## Description

Builds Apple test targets and runs XCTest or Swift Testing tests through the
generic Struo test capability. Attributes whose names contain `sdk` configure
the
[Apple software development kit (SDK)](https://developer.apple.com/documentation/xcode)
used for the build.

On macOS, XCTest bundles run through Xcode's `test-without-building` harness
using a generated test-run configuration and the bundle compiled by Struo.
This applies with or without an application host. Compilation, linking, and
configuration materialization remain cacheable; test execution still runs fresh.
Swift Testing-only bundles continue to use their structured event-stream runner.

Tests do not need to repeat framework dependencies that belong to a plugin or
library in their dependency graph. Struo links only the direct framework
boundary and stages the full runtime framework closure inside the test bundle
before signing it.

Resources follow Xcode bundle placement. iOS test resources are placed at the
root of the test bundle, while macOS test resources are placed under
`Contents/Resources`. Structured resource roots preserve their own directory
name. Custom property-list templates can expand build-setting placeholders,
including the absolute source root supplied by an Xcode project adapter.

The compiler receives both the XCTest framework search path and the platform
developer library search path. The latter contains XCTest's Swift module
overlay and its Swift support library, which are required for Swift assertion
helpers such as `XCTFail`.

When the dependency graph contains one non-extension application provider, the
test linker uses that application's executable as its bundle loader. This resolves code
under test from the host without copying the host's application objects into
the test bundle.

The `test_tools` dependency role stages command-line executables beside the
test bundle. These tools are runtime dependencies, not linked code or test
host applications.

Interface tests use the platform test-runner application. Struo packages and
signs the runner, nests the interface-test bundle under its `PlugIns`
directory, stages the required testing frameworks, and launches the declared
application under test. Compilation, linking, packaging, and signing remain
Struo build actions.

A bundle that depends on a Swift macro links the macro and imports its module,
so code guarded by `canImport` of the macro module compiles. A macro only ever
builds for the host, so a test bundle that depends on one builds for the host
as well, along with everything it depends on.

## Reported Results

A Swift Testing bundle reports every test and what became of it. The testing
library records the run and Struo turns that record into normalized results, so
a test that was filtered out, skipped, or never reached is reported as such
rather than as a pass, and an issue the test marked as known is not a failure.

Where results come from the XCTest host, Struo translates the host's per-case
completion records into normalized outcomes and durations. This includes Swift
Testing cases that the host runs alongside XCTest cases. Struo also lists cases
found in sources so a shard can address them; a case without a completion
record stays unknown rather than being credited with a pass. When XCTest
reports a completed `All tests` or `Selected tests` suite, passed or failed, and its total matches the
observed case records, that native inventory supersedes source declarations,
excluding platform-conditional cases that were not compiled. Missing or
inconsistent completion summaries retain unknown cases and fail closed.
A bundle stays with the XCTest host when it holds XCTest cases, which only that host runs, and
when it runs anywhere other than macOS.

On macOS, XCTest bundles honor `skipped_tests` and exact case filters through
the Xcode harness's native selection arguments.

## Coverage-assisted Selection

Set `coverage_per_test = true` and declare `coverage_sources` as product source
globs, separate from the bundle's test registration sources. Instrument those
product dependencies with Swift's `-profile-generate -profile-coverage-mapping`
or Clang's `-fprofile-instr-generate -fcoverage-mapping` flags. The test bundle
itself is instrumented automatically. Python 3 and Xcode's LLVM tools convert
the profile to normalized observations.

This mode declares the complete bundle invocation as one atomic unit named
`<target>::invocation`. Fresh discovery builds the bundle without running its
tests. Coverage belongs to that invocation, never to each native case inside
it. Exact requests accept only the complete invocation unit. A skipped,
unknown or failed case, missing profile, uninstrumented declared product source
or observed child process makes the observation incomplete and retains graph
selection. A process hook must load exactly once; runners that prevent loading
it cannot establish complete evidence. Device and multiprocess interface-test
execution cannot currently establish complete observations.

## Attributes

`explicit_modules` is a boolean, defaulting to `false`, that enables
compiler-scanned, cacheable Swift and Clang module actions.
`dependency_check` accepts `"off"` (the default) or `"error"`; checking
requires explicit modules. See [explicit modules](/guide/graph/apple#explicit-modules-and-dependency-checks)
for native propagation, dependency errors, and current limitations.
The resolver-owned `_declared_deps` metadata preserves declarations before
import inference and should not be authored manually.

| Attribute | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `platform` | string | yes |  | Apple platform for the tests |
| `minimum_os` | string | no | `13.0` | Minimum supported operating system version |
| `target_sdk_version` | string | no | `minimum_os` | Software development kit version used in the target triple |
| `sdk_variant` | string | no | `simulator` | `simulator` or `device`; ignored on macOS (not configurable) |
| `xcode_developer_dir` | string | no | active Xcode | Xcode developer directory used to resolve build tools |
| `product_name` | string | no | target name | Test bundle product name (not configurable) |
| `bundle_id` | string | no | `dev.struo.tests.<product_name>` | Test bundle identifier (not configurable) |
| `test_host` | target | no |  | Application target hosting the test bundle |
| `resources` | list&lt;string&gt; | no | `[]` | Resource glob patterns bundled into the test bundle |
| `structured_resources` | list&lt;string&gt; | no | `[]` | Resource directory roots whose own basename is preserved inside the test bundle |
| `asset_catalogs` | list&lt;string&gt; | no | `[]` | Asset catalog paths compiled into the test bundle |
| `info_plist` | string | no |  | Info.plist template path |
| `info_plist_substitutions` | map&lt;string,string&gt; | no | `{}` | Build-setting values substituted into property-list placeholders |
| `entitlements` | string | no |  | Entitlements plist path |
| `destination` | string | no |  | Simulator, device, or local destination selector |
| `device_type` | string | no |  | Simulator device type to run on, as listed by `xcrun simctl list devicetypes`, such as `iPad Air 11-inch (M2)`; defaults per platform, see [Simulator](#simulator) |
| `os_version` | string | no |  | Simulator runtime version to run on, as listed by `xcrun simctl list runtimes`, such as `18.2`; defaults to the runtime matching the build SDK |
| `test_plan` | string | no |  | XCTest plan path |
| `test_env` | map&lt;string,string&gt; | no | `{}` | Environment variables passed to the test runner |
| `test_arguments` | list&lt;string&gt; | no | `[]` | Arguments passed to the test process |
| `skipped_tests` | list&lt;string&gt; | no | `[]` | Suite or case identifiers excluded from the test run |
| `sdk_frameworks` | list&lt;string&gt; | no | `[]` | Apple software development kit frameworks linked by name |
| `weak_sdk_frameworks` | list&lt;string&gt; | no | `[]` | Apple software development kit frameworks linked weakly |
| `sdk_dylibs` | list&lt;string&gt; | no | `[]` | Apple software development kit dynamic libraries linked by name |
| `linkopts` | list&lt;string&gt; | no | `[]` | Extra linker flags |
| `swift_flags` | list&lt;string&gt; | no | `[]` | Extra Swift compiler flags |
| `clang_flags` | list&lt;string&gt; | no | `[]` | Extra Clang compiler flags for C, C++, Objective-C, and Objective-C++ test sources |
| `per_source_clang_flags` | map&lt;string,string&gt; | no | `{}` | JSON-encoded Clang compiler flag lists keyed by test source path |
| `defines` | list&lt;string&gt; | no | `[]` | Compatibility conditions passed to both Swift and Clang |
| `swift_defines` | list&lt;string&gt; | no | `[]` | Swift conditional compilation conditions |
| `clang_defines` | list&lt;string&gt; | no | `[]` | C-family preprocessor definitions |
| `exported_header_dirs` | list&lt;string&gt; | no | `[]` | Header search directories exported by the test target |
| `private_header_dirs` | list&lt;string&gt; | no | `[]` | Private header search directories used while compiling tests |
| `private_headers` | list&lt;string&gt; | no | `[]` | Private header files required while compiling tests |
| `bridging_header` | string | no |  | Objective-C bridging header imported into Swift test sources |
| `prefix_header` | string | no |  | Prefix header included before every C-family test source |
| `prebuild_actions` | list&lt;string&gt; | no | `[]` | Adapter-owned serialized build preparation actions that run before compilation. Records may opt into caching when they declare complete inputs and outputs; always-run records remain uncached |
| `swift_testing` | bool | no | `false` | Run sources that use Swift Testing (`import Testing`) through the generic Struo test capability |
| `ui_testing` | bool | no | `false` | Package the bundle inside the platform test runner and launch an application under test |
| `labels` | list&lt;string&gt; | no | `[]` | Agent-readable labels used for filtering or policy |

The `prepackage_actions` attribute accepts an ordered `list<string>` of
serialized script records, defaulting to `[]`. These run after linking and
before resource processing, so generated resources feed packaging.

The `postbuild_actions` attribute accepts an ordered `list<string>` of
serialized script records, defaulting to `[]`. These actions run after
product assembly and before final signing. Complete declarations may be
cached; untracked scripts rerun and publish changes to known product files.
See [native script phases](/docs/guide/graph/apple/xcode#script-build-phases)
for mapped build settings and file-list variables.

## Dependency Edges

| Edge | Accepts | Description |
| --- | --- | --- |
| `deps` | `apple_linkable`, `apple_application`, `apple_swift_plugin`, `native_linkable`, `apple_generated_files` | Code under test, optional host application, native linkables, compiler plugins, and generated package files |
| `test_tools` | `apple_application`, `apple_executable` | Command-line executables staged beside the test bundle without linking or selecting a test host |

## Providers

The target emits `apple_test_bundle`, `apple_bundle`, and `struo_test_info`.

## Capabilities

| Capability | Output groups | Requires |
| --- | --- | --- |
| `build` | `default`, `bundle`, `dsyms` |  |
| `test` | `default`, `test_results`, `coverage` |  |

## Outputs

| Output | Location |
| --- | --- |
| Test bundle | `.struo/out/<target>/<product_name>.xctest` |
| C-family objects | `.struo/out/<target>/Objects/*.o` for mixed-language test targets |
| macOS test binary | `.struo/out/<target>/<product_name>.xctest/Contents/MacOS/<product_name>` |
| Other Apple platform test binary | `.struo/out/<target>/<product_name>.xctest/<product_name>` |
| iOS interface-test runner | `.struo/out/<target>/<product_name>-Runner.app` |
| iOS interface-test bundle | `.struo/out/<target>/<product_name>-Runner.app/PlugIns/<product_name>.xctest` |
| macOS property list | `.struo/out/<target>/<product_name>.xctest/Contents/Info.plist` |
| Other Apple platform property list | `.struo/out/<target>/<product_name>.xctest/Info.plist` |
| macOS resources | `.struo/out/<target>/<product_name>.xctest/Contents/Resources` |
| Other Apple platform resources | `.struo/out/<target>/<product_name>.xctest` |
| Compiled asset catalog | `Assets.car` in the platform-specific resource location |
| Runtime frameworks | The test bundle's `Frameworks` directory when dependencies require them |
| macOS code signature | `.struo/out/<target>/<product_name>.xctest/Contents/_CodeSignature/CodeResources` |
| Other Apple platform code signature | `.struo/out/<target>/<product_name>.xctest/_CodeSignature/CodeResources` |
| Test results | `.struo/out/<target>/test/test_results.json` after `struo test` |
| Test log | `.struo/out/<target>/test/swift-testing.log` for Swift Testing or `xctest.log` for XCTest |
| Native runner output | `.struo/out/<target>/test/native_results.txt` |

## Simulator

Simulator test runs use a simulator that Struo owns, as the Apple Bazel rules'
test runner does, named after its device type
and runtime, such as `Struo iPhone 15 (iOS 26.5)`. Struo creates it on first
use and reuses it on later runs. The device type comes from `device_type`, or
defaults to iPhone 15 for iOS, Apple Watch Series 9 (45mm) for watchOS,
Apple TV 4K (3rd generation) for tvOS, and Apple Vision Pro for visionOS. The
runtime comes from `os_version`, or defaults to the installed runtime that
matches the SDK the target was built with, then the newest runtime for the
platform. A missing `os_version` runtime fails the run instead of falling back.
Set `STRUO_APPLE_SIMULATOR_UDID` to run on a specific simulator instead.

```toml
[target.attrs]
platform = "ios"
device_type = "iPad Air 11-inch (M2)"
os_version = "18.2"
```

## Limitations

Direct `test_host` attributes, entitlements, destinations, and test plans are
unsupported. Application hosts discovered through dependency providers are
supported. Test execution is limited to macOS logic tests, iOS simulator unit
tests, and iOS simulator interface tests.

## Example

```toml
[[target]]
name = "AppTests"
kind = "apple_test_bundle"
srcs = ["AppTests/Sources/*.swift"]

[target.attrs]
platform = "macos"
swift_testing = true
labels = ["swift-testing"]
```
