背景
在使用xcode运行代码后,查看运行日志,可以看到在文件夹(macOS系统)下 *xxx为用户名,zzz为项目名
/Users/xxx/Library/Developer/Xcode/DerivedData/zzz-fnntnbtgxuuirhgjlfpsminbsfiz/Logs/Test
产生了后缀为.xcresult的运行文件 
查看xcresult文件
方法1:找到该文件将其拖入xcode,可以得到跟运行日志一样的结果 方法2:通过命令行进行解析 通过以下命令运行项目,可以指定xcresult文件的生成路径
xcodebuild -project XCCov-Demo.xcodeproj/ -scheme XCCov-Demo -derivedDataPath Build/ -destination 'platform=iOS Simulator,OS=12.2,name=iPhone X?' -enableCodeCoverage YES clean build test
- enableCodeCoverage:允许代码覆盖率(需要YES)
- derivedDataPath:测试中产生的文件存放位置(会自动生成文件夹)
- project:项目配置,找到xxx.xcodeproj
- scheme:与项目shecme一致
- destination:平台和机型,真机调试填iOS,设备名称,如 “platform=iOS,name=wrip7”
模拟器需要注明模拟器,如
“platform=iOS Simulator,OS=13.4,name=iPhone 11”
配置
xcode 11+
使用xparse提取xcresult中所需文件
Apple 从 Xcode 11 开始更改了 xcresult 的格式。现在为了从中取出所需内容,必须通过 Xcode 的 xcresulttool ( xcrun xcresulttool)来实现,但这可能非常麻烦(如果有大量测试用例,它可能是 xcresulttool 中的数百个命令)。
xparse通过自动执行 xcresulttool 调用和解析来简化必须执行的过程。
安装xparse
*如果提示 command not found, 使用以下命令安装xparse
brew install chargepoint/xcparse/xcparse
使用xparse
当执行完以下命令运行项目后
xcodebuild -project XCCov-Demo.xcodeproj/ -scheme XCCov-Demo -derivedDataPath Build/ -destination 'platform=iOS Simulator,OS=12.2,name=iPhone X?' -enableCodeCoverage YES clean build test
这将在路径Build/Logs/Test/Run-{projectName_timestamp}生成代码覆盖率数据。xcresult/3_Test我们将看到代码覆盖。xccovreport和。xccovarchive扩展名。覆盖率报告包含每个目标、源文件和具有覆盖率信息的函数/方法的行覆盖率百分比。覆盖率归档包含报告中每个文件的原始执行计数。
提取附件(截图等)
以下命令可以导出
xcparse attachments /path/to/Test.xcresult /path/to/outputDirectory --uti public.plain-text public.image
导出代码覆盖数据
这将导出.xccovreport 和 .xccovarchive到输出目录。
xcparse codecov /path/to/Test.xcresult /path/to/exportCodeCoverageFiles
导出日志
这将导出日志和诊断文件到每个操作的文件夹结构类似于Xcode 10的xcresult格式。
xcparse logs /path/to/Test.xcresult /path/to/exportLogFiles
转换格式
这将导出给定应用程序大小报告的JSON表示到输出目录。
xcparse convert /path/to/AppThinningSizeReport.txt /path/to/outputDirectory --flagVariants sizeLimit
使用xccov查看代码覆盖数据
当使用xcparse导出代码覆盖数据后,可以使用xccov命令解析进一步解析
通过文本查看报告
xcrun xccov view Build/Logs/Test/xxxxx.xcresult/3_Test/action.xccovreport
通过JSON格式查看报告
xcrun xccov view --json Build/Logs/xxxxx.xcresult/3_Test/action.xccovreport
列出适用于代码覆盖的文件
xcrun xccov view --file-list Build/Logs/Test/xxxxxx.xcresult/3_Test/action.xccovarchive
展示具体文件的覆盖数据
xcrun xccov view --file ~/Desktop/XCCov-Demo/XCCov-Demo/AppDelegate.swift Build/Logs/Test/xxxxxx/3_Test/action.xccovarchive/\
合并报告
xcrun xccov merge --outReport ~/Desktop/out.xccovreport --outArchive ~/Desktop/out.acarchive Build/Logs/Test/Run-XCCov-Demo-2019.04.03_07-40-58-+0100.xcresult/3_Test/action.xccovreport Build/Logs/Test/Run-XCCov-Demo-2019.04.03_07-40-58-+0100.xcresult/3_Test/action.xccovarchive/ Build/Logs/Test/Run-XCCov-Demo-2019.04.03_07-52-58-+0100.xcresult/3_Test/action.xccovreport Build/Logs/Test/Run-XCCov-Demo-2019.04.03_07-52-58-+0100.xcresult/3_Test/action.xccovarchive/
|