美文网首页
iOS 测试 iOS-客户端代码覆盖测试 (Gcov)

iOS 测试 iOS-客户端代码覆盖测试 (Gcov)

作者: 00after | 来源:发表于2019-10-29 18:10 被阅读0次

原文
iOS-客户端代码覆盖测试 (Gcov)
iOS-客户端代码覆盖测试 (Gcov)

本文主要介绍如何对 iOS 客户端项目集成代码覆盖率检测,使用了 Gcov 工具来完成。

*主要支持 Objective-C(目前貌似不支持Swift)

*适合大部分代码为 OC 语言编写的项目。

编辑Podfile文件, 添加XcodeCoverage库
打开终端, cd到项目路径编辑Podfile

vi Podfile

添加:

pod 'XcodeCoverage', ‘~>1.0'

运行pod install安装依赖库

pod install

一、项目集成

1.项目设置

Generate Legacy Test Coverage Files

Instrument Program Flow

把这两项参数设置成 YES,找不到的话请看图(注意细节)

image

image

为了能够在真机上把文件取出来,需要配置 plist 文件打开文件共享

该设置允许 iTunes 或者 Xcode、电脑助手等软件看到该 App 的沙盒中的共享文件目录。

image

2.配置 Gcov

在 AppDelegate.m 的 didFinishLaunchingWithOptions 函数中,加入以下代码:

NSString *covFilePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/xperia_coverage_files"]; setenv("GCOV_PREFIX", [covFilePath cStringUsingEncoding: NSUTF8StringEncoding], 1); setenv("GCOV_PREFIX_STRIP", "1", 1);

第一行代码是设置代码覆盖的结果文件放在 App 沙盒文件目录的位置(Documents 下面新建 xperia_coverage_files 文件夹)

3.在需要采集覆盖率的地方加入代码

ps: 也就是执行完测试之后要走的代码,目的是将代码执行情况刷入文件存储起来。

extern void __gcov_flush(void);__gcov_flush();

比如在 viewDidload()函数或者 app 准备切到后台的时候来采集。

二、结果数据收集

4.采集 gcda 文件

连接 Xcode,打开 Window -> Devices and Simulators

找到对应设备的对应 App 的 container 内容,进行下载:

image

下载沙盒文件

image

下载完 container 后右键显示包内容

5.采集 gcno 文件

image

打开 Finder,按下 Shift+Command+H,然后

逐步进入:/Users/lipeng/Library/Developer/Xcode/DerivedData/CodeCov-fjkssrrlmpdspdgtdoyyhsuhrocu/Build/Intermediates.noindex/CodeCov.build/Debug-iphoneos/CodeCov.build/Objects-normal/arm64

项目名称可能不同,通过文件夹修改时间也可以确认是在哪个文件夹下。

补充说明 :
路径在:

进入项目目录/Pods/XcodeCoverage, 打开env.sh, 找到 OBJECT_FILE_DIR_normal属性和CURRENT_ARCH属性的值, 这里要注意下如果CURRENT_ARCH的值是undefined就改成arm64

6.将所有的 gcda 文件和 gcno 文件放到一个文件夹下。

Lcov的使用

7.如果 Mac 上命令行输入 lcov 提示 command not found,也就是没有安装 lcov

lcov 官网 http://ltp.sourceforge.net/coverage/lcov.php

可以直接通过

brew install lcov

来安装 lcov。

要是连 homebrew 都没了解,那我不解释了。。。🙂

8.合成采集的代码覆盖信息到输出文件

lcov -c -d . -o myGcovResult.info

9.生成 html 文件

当前所在文件夹是放好 gcno 和 gcda 的 CodeCoverageFiles 文件夹。

genhtml -o html myGcovResult.info

结果如图所示:

image

命令行结果

image

文件目录截图
打开 html 文件夹中的 index.html

A.全局结果

image

B.单个文件覆盖率

image

(温馨提示:点击各个文件名可以进入到详情)

Congratulations !🎖🎖🎖

未完待续:

app 将 gcno 文件打包,gcda 文件打包,上传到服务器。

在服务器上生成对应的代码覆盖结果。


代码 Demo 区,展示几个关键函数

CodeCoverageTool.m 文件的部分内容

#import "CodeCoverageTool.h"

@interface LDCodeCoverage()

@end

@implementation CodeCoverageTool

- (instancetype)init {
    self = [super init];

    if (self) {
        self.mode = CodeCoverageEnabled;
    }

    return self;
}

+ (instancetype)sharedManager {
    static CodeCoverageTool *ct = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        ct = [[[self class] alloc] init];
    });

    return ct;
}

// 主要生成代码覆盖数据的函数
- (void)flush {
    if (![self enabled]) {
        return ;
    }

  // 确认不是iPhone模拟器,然后设置代码覆盖结果文件的存储路径
#if !TARGET_IPHONE_SIMULATOR
    NSString *covFilePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/xperia_coverage_files"];
    setenv("GCOV_PREFIX", [covFilePath cStringUsingEncoding: NSUTF8StringEncoding], 1);
    setenv("GCOV_PREFIX_STRIP", "13", 1);
#endif

    [self flushCodeCoverage];
}

// 是否开启了代码覆盖开关
- (BOOL)enabled {
    return (self.config && (self.mode != CodeCoverageDisabled));
}

// 文中提到的写入覆盖数据的核心代码,封装成该函数
- (void)flushCodeCoverage {
    extern void __gcov_flush(void);
    __gcov_flush();
}

补充:
8.合成采集的代码覆盖信息到输出文件

lcov -c -d . -o myGcovResult.info

9.生成 html 文件
当前所在文件夹是放好 gcno 和 gcda 的 CodeCoverageFiles 文件夹。

genhtml -o html myGcovResult.info

单独产生同一个程序不同用例的info并合并

make
#运行两个参数用例并产生info文件和html文件
./helloworld  i 2
lcov -c -d . -o helloworld2.info
genhtml -o 222 helloworld2.info

#运行无参数用例并产生info文件和html文件
rm helloworld_gcov.gcda 
./helloworld
lcov -c -d . -o helloworld1.info
genhtml -o 111 helloworld1.info 

#合并两个用例产生的info文件,输出同一个模块不同用例的总的统计数据
genhtml -o 333 helloworld1.info helloworld2.info 

注意:整合多人的测试覆盖率

cd /Users/jay/Tools/lcov-master/bin/lcov(lcov工具的文件目录)

lcov -a ~/Desktop/1.info(第一个info文件路径) -a ~/Desktop/2.info(第二个info文件路径) -a… -o ~/Desktop/combine.info(合并后的info文件路径)

相关文章

网友评论

      本文标题:iOS 测试 iOS-客户端代码覆盖测试 (Gcov)

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