美文网首页
iOS 单元测试覆盖率统计 lcov

iOS 单元测试覆盖率统计 lcov

作者: 海绵大虾 | 来源:发表于2019-03-20 14:34 被阅读0次

iOS 单元测试覆盖率统计脚本

1、首先安装 lcov
brew install lcov

安装 lcov 的同时,会安装依赖 genhtml

分析 xcode 代码覆盖率文件,并生成可视化的 html

2、工程配置

Scheme 配置

Test 选项 Code Coverage 勾选

Build settings 设置

instrument program flow = yes
generate legacy test coverage files = yes
3、开始单元测试

执行全部单元测试方法,模拟器执行测试

xcodebuild test -scheme schemeName -target targetName -destination 'platform=iOS Simulator,name=iPhone 7 Plus,OS=12.1'
4、拷贝单元测试覆盖率文件至目标文件夹

从 xcodeproj 文件获得覆盖率文件夹的绝对路径

project_setting=$(xcodebuild -showBuildSettings -scheme $scheme_name -project $projectpath -json)
echo $project_setting | jq -r '.[0].buildSettings.PROJECT_TEMP_DIR' > $outputpath/setting.txt
project_setting_out=`cat $outputpath/setting.txt`
detail_path="Debug-iphonesimulator/$scheme_name.build/Objects-normal/x86_64"
project_output_dir=$project_setting_out/$detail_path

从获得的绝对路径拷贝文件至临时文件夹

cp $project_output_dir/*.gcno $outputpath/tmp
cp $project_output_dir/*.gcda $outputpath/tmp

for file_name in ${exit_file_name[@]};do
    echo $file_name
    rm $outputpath/tmp/$file_name
done
echo 'output'

project_dir=$(pwd)

exit_file_name 是一个需要过滤文件的数组,不想统计覆盖率文件,可以按照 xxxx.* 放入数组

outputpath 是导出的结果文件路径,默认是在当前目录下

exit_file_name=('SV*')
outputpath='./cover_output' 
5、生成可视化的覆盖率统计表
lcov -c -d $outputpath/tmp -b $project_dir -o $outputpath/coverage.info

genhtml -t 单元测试报告 $outputpath/coverage.info -o $outputpath

6、完整脚本地址及使用方法

代码

使用方法:

按照配置之后,使用命令或者手动单元测试之后,将 report.sh 文件放入 xcodeproj 同级目录,使用如下命令获得覆盖率报告

sh report.sh -p xxx.xcodeproj -s scheme

相关文章

网友评论

      本文标题:iOS 单元测试覆盖率统计 lcov

      本文链接:https://www.haomeiwen.com/subject/kmizmqtx.html