You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何强制iOS项目开发者推送代码前运行并通过单元测试?

Enforcing Unit Test Passes for iOS Projects Before Push (SourceTree + Bamboo)

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/hooks folder (enable hidden files in Finder if needed).
  • Rename the existing pre-push.sample file to pre-push (remove the .sample extension).
  • Open the pre-push file 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-push in 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 .git isn't tracked, share this hook with your team by adding a git-hooks folder to your project root (e.g., project-root/git-hooks/pre-push) and include a setup script to copy it to .git/hooks for 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 xcodebuild command 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 xcodebuild command).
  • 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:19:39