Parallelize Unit Tests in Your iOS App
· Fastlane - from zero to hero
Introduction
In this post, I’ll show you two ways to run unit tests in your iOS app: using the Xcode CLI with Bash and using Fastlane. Both approaches have their pros and cons, and you can choose the one that best fits your workflow. Whether you’re working in a CI/CD environment or running tests locally, these methods will help you automate your testing process.
Prerequisites
For both methods, you’ll need:
- Xcode installed
- A working iOS app with unit tests
For the Fastlane method, you’ll also need:
- Fastlane installed
Method 1: Using Xcode CLI and Bash
This method uses the Xcode CLI (xcodebuild
) and a Bash script to run your tests. It’s lightweight and doesn’t require any third-party tools.
Script
Here’s the script to run your unit tests:
#!/bin/bash
set -u # Treat unset variables as errors
# Configurable variables
SCHEME="YourSchemeName"
DESTINATION="platform=iOS Simulator,name=iPhone 16 Pro,OS=latest"
PROJECT="YourProject.xcodeproj"
# Run tests and capture exit code
set -o pipefail
xcodebuild \
-scheme "$SCHEME" \
-project "$PROJECT" \
-destination "$DESTINATION" \
-parallel-testing-enabled YES \
-parallel-testing-worker-count 4 \
clean test | tee xcodebuild.log | xcpretty
EXIT_CODE=${PIPESTATUS[0]}
if [ "$EXIT_CODE" -ne 0 ]; then
echo "❌ Unit tests failed!"
exit "$EXIT_CODE"
else
echo "✅ All unit tests passed!"
fi
Running the Script
Save the script to a file (e.g., run_tests.sh
), make it executable, and run it:
chmod +x run_tests.sh
./run_tests.sh
This will run your tests and display the results in the terminal.
Method 2: Using Fastlane
Fastlane is a popular tool for automating iOS and Android development tasks. It provides a simple way to run tests and integrates well with CI/CD pipelines.
Setting Up Fastlane
-
Install Fastlane:
brew install fastlane
-
Navigate to your project directory and initialize Fastlane:
fastlane init
Follow the prompts to set up Fastlane for your project.
Adding a Test Lane
Edit your Fastfile
to add a lane for running tests:
default_platform(:ios)
platform :ios do
desc "Run unit tests"
lane :run_tests do
scan(
scheme: "YourSchemeName",
devices: ["iPhone 16 Pro"],
clean: true,
parallel_testing: true,
concurrent_workers: 4,
output_directory: "./fastlane/test_output",
output_types: "html,junit"
)
end
end
Running the Test Lane
Run the test lane using the following command:
fastlane run_tests
Fastlane will execute your tests and generate a report in the ./fastlane/test_output
directory.
Comparison: Xcode CLI vs. Fastlane
Feature | Xcode CLI + Bash | Fastlane |
---|---|---|
Setup | Minimal | Requires Fastlane setup |
Ease of Use | Requires scripting | Simple, declarative syntax |
Output | Customizable with xcpretty | Built-in reports (HTML, JUnit) |
CI/CD Integration | Manual | Seamless |
Flexibility | High | High |
Conclusion
Both methods are effective for running unit tests in your iOS app. If you prefer a lightweight, script-based approach, the Xcode CLI with Bash is a great choice. If you want a more feature-rich solution with built-in reporting and CI/CD integration, Fastlane is the way to go.
Try both methods and see which one works best for your workflow. Let me know if you have any questions or need help setting up either approach. Happy testing!