Best practices
For a better understanding, we will use the following example project structure:
MainApp
├── Framework1 ── Framework2
├── Framework2
└── Framework3
Framework1Tests ── Framework1
Framework2Tests ── Framework2
Framework3Tests ── Framework3Building and testing a single test target
Setup
- Enable automatic scheme generation in the
Projectmanifest viaautomaticSchemesOptionsso that a scheme is generated for every target. UsetargetSchemesGrouping: .notGroupedto generate a separate scheme per target:
let project = Project(
name: "Framework1",
options: .options(
automaticSchemesOptions: .enabled(
targetSchemesGrouping: .notGrouped
)
),
targets: [...],
)Learn more about scheme generation here
- Generate project with cache
geko generate --no-open --cacheCommands
Build a single test target:
geko build Framework1Tests
// or
geko test --build-only Framework1TestsRun a single test target:
geko test Framework1TestsRun a single test from a test target:
geko test Framework1Tests --test-targets Framework1Tests/MyPublicClassTests/testHelloThis approach optimizes build time by building only the needed target and its dependencies (without building the main target and its dependencies):
Framework1 ── Framework2
Framework1Tests ── Framework1Building and testing multiple test targets
Setup
- For this you need to generate a test plan with all test targets and add it to the scheme of the main application target:
let project = Project(
name: "MainApp",
targets: [...],
schemes: [
Scheme(
name: "MainApp",
testAction: .testPlans([
.generated(
name: "GeneratedTestPlan.xctestplan",
directory: "TestPlans/Geko",
targetSelection: [.all()]
)
])
)
]
)Learn more about test plan generation here
- Generate project with cache
geko generate --no-open --cacheCommands
Build without running tests:
geko test MainApp --build-only --test-plan GeneratedTestPlan.xctestplanRun the entire test plan:
geko test MainApp --test-plan GeneratedTestPlan.xctestplanIn this case the whole test plan is built and run, as expected:
MainApp
├── Framework1 ── Framework2
├── Framework2
└── Framework3
Framework1Tests ── Framework1
Framework2Tests ── Framework2
Framework3Tests ── Framework3But if you only need to build and run specific test targets:
geko test MainApp --test-plan GeneratedTestPlan.xctestplan --test-targets Framework1Tests Framework2TestsThen the extra test target Framework3Tests is still built, even though it is not run:
MainApp
├── Framework1 ── Framework2
├── Framework2
└── Framework3
Framework1Tests ── Framework1
Framework2Tests ── Framework2
Framework3Tests ── Framework3Problem: unnecessary test targets are built from the test plan
Xcode builds all the test targets listed in the test plan. Even if you explicitly specify which test targets to run via the -only-testing parameter, Xcode still builds all test targets from the test plan. Extra test targets can also pull in the build of extra modules (for example mocks, etc.), which can significantly hurt build time when there are many targets in the focus.
Solution
A --edit-test-plan flag was added to the geko test command, which enables editing of the test plan. On each run of geko test, the list of targets passed via --test-targets is written into the test plan. If the --test-targets list is empty, all available test targets (or only those in the focus) are added to the test plan.
When running the previous command with the --edit-test-plan flag:
geko test MainApp --test-plan GeneratedTestPlan.xctestplan --edit-test-plan --test-targets Framework1Tests Framework2Teststhe project build graph becomes more optimal and looks like this:
MainApp
├── Framework1 ── Framework2
├── Framework2
└── Framework3
Framework1Tests ── Framework1
Framework2Tests ── Framework2