准备
安装slather
实际上slather的作用就是:
Generate test coverage reports for Xcode projects & hook it into CI.
gem install slather
创建.slather.yml配置文件
workspace: Example/YourProject.xcworkspace
xcodeproj: Example/YourProject.xcodeproj
scheme: YourProject-Example
source_directory: YourProject/Classes
output_directory: ./coverage_report
ignore:
- Example/*
没有workspace则可以去掉
使用
首先用这个命令跑出Coverage
time xcodebuild test -workspace $CI_PROJECT_NAME.xcworkspace -scheme $CI_PROJECT_NAME-Example -destination 'platform=iOS Simulator,name=iPhone 6,OS=10.0' -enableCodeCoverage YES | xcpretty
接着写个coverage.sh
MIN_COVERAGE_RATE=$1
if [[ ! $MIN_COVERAGE_RATE ]]; then
MIN_COVERAGE_RATE='75'
fi
echo "minimum converage rage:$MIN_COVERAGE_RATE"
COVERAGE=$(slather coverage -s | grep -o -E '(\d+\.\d+)%' | tail -1 | cut -d '%' -f1)
if ! echo "$COVERAGE $MIN_COVERAGE_RATE -p" | dc | grep > /dev/null ^-; then
echo "current:$COVERAGE > min:$MIN_COVERAGE_RATE"
echo 'pass'
else
echo "current:$COVERAGE < min:$MIN_COVERAGE_RATE"
echo 'no pass'
exit 1
fi
这样你就可以在ci中简单的使用这个shell脚本来限制测试覆盖率需要达到多少才算合格了。