如何强制iOS项目开发者推送代码前运行并通过单元测试?
Great question—this is a super common pain point for iOS teams using Git-based workflows and CI tools like Bamboo. Below are layered, actionable solutions to ensure unit tests compile and pass before code makes it to your remote repo or triggers a Bamboo failure:
1. Local Git Pre-Push Hook (First Line of Defense)
Git hooks let you run scripts automatically before key actions like pushing. This will block pushes immediately if tests fail, right from SourceTree.
Setup Steps:
- Navigate to your project's hidden
.git/hooksfolder (enable hidden files in Finder if needed). - Rename the existing
pre-push.samplefile topre-push(remove the.sampleextension). - Open the
pre-pushfile in a text editor and replace its content with this script (adjust for your project's scheme/simulator):
#!/bin/sh # Define your project's scheme and test destination SCHEME="YourAppScheme" DESTINATION="platform=iOS Simulator,name=iPhone 15" # Run unit tests echo "Running unit tests before push..." xcodebuild test -scheme "$SCHEME" -destination "$DESTINATION" -quiet # Check if tests passed if [ $? -ne 0 ]; then echo "❌ Unit tests failed! Push aborted." exit 1 fi echo "✅ Unit tests passed. Proceeding with push..." exit 0
- Save the file and make it executable: run
chmod +x .git/hooks/pre-pushin Terminal.
Key Notes:
- SourceTree will automatically run this hook when you try to push. If tests fail, the push will be blocked with an error message.
- Since
.gitisn't tracked, share this hook with your team by adding agit-hooksfolder to your project root (e.g.,project-root/git-hooks/pre-push) and include a setup script to copy it to.git/hooksfor new members. - Developers can bypass this with
git push --no-verify, so pair this with remote protections below.
2. SourceTree Custom Action (Optional, for Team Consistency)
For teams that rely heavily on SourceTree, create a custom action to run tests with one click, making it harder to forget:
- Open SourceTree → Preferences → Custom Actions → Add.
- Name it "Run Unit Tests & Prepare for Push".
- Set the script to the same
xcodebuildcommand from the pre-push hook. - Assign a keyboard shortcut (e.g., Cmd+Shift+T) for quick access.
- Encourage your team to run this action before every push.
3. Remote Branch Protection (Last Line of Defense)
Even if local hooks are bypassed, remote repo rules will block untested code from merging into protected branches (like main or develop):
- If using GitHub/GitLab/Bitbucket (all supported by SourceTree):
- Go to your repo's Settings → Branch Protection.
- Enable rules for your main branches:
- Require pull requests before merging.
- Require status checks to pass before merging: add your Bamboo unit test pipeline as a required check.
- This ensures only code that passes Bamboo's unit tests can be merged into critical branches, preventing failed builds entirely.
4. Bamboo Pipeline Optimization
Tweak your Bamboo pipeline to fail fast if tests don't pass:
- Set your pipeline's first stage to run unit tests (using the same
xcodebuildcommand). - Configure the stage to terminate immediately if tests fail, with a clear notification to the developer who pushed the code.
- This reduces wasted CI resources and gives immediate feedback even if code slips past local checks.
内容的提问来源于stack exchange,提问作者mars_dev

