如何优雅的写C++代码 Obotcha介绍(简单检测单元测试覆盖率的方法)

Obotcha已经写了快4年的,大约有44K行,4K个接口。对应的单元测试集(GitHub - wangsun1983/ObotchaTestSuite)目前已经基本涵盖了大多数的模块,但是一直没办法确定有多少接口没有被测到。所以写了个简单的python脚本去做了一个统计。方法如下:

1.首先通过Objdump解析libobothca.so(obotcha的编译输出),获得所有obotcha的接口数据,存入functionMap中。

def analyseObjdump():
    for line in fileinput.input("../Report/CoverageReport/obotchadump.txt"):
        if line.find("g ") > 0 and line.find("obotcha") >0 and line.find(".text") > 0:
            #print(line)
            index = line.rfind(" ")
            function = line[index+1:len(line) - 1]
            #print("add function [",function,"]")
            functionMap[function] = 1


2.在ObotchaTestSuit中,逐个文件夹编译,
然后用objdump去解析编译产物(一般就是测试的bin)

3.将步骤2中统计到的obotcha接口函数从mapA中移除

def scan(path):
    print("path is ",path)
    isMakeFileExist = False

    for filename in os.listdir(path):
        if filename == "makefile":
            print("i find makefile:",path)
            isMakeFileExist = True
            break
    
    print("isMakeFileExist is ",isMakeFileExist)

    if isMakeFileExist:
        print("start make")
        makeret = os.popen("cd " + path + " && make 2>&1").read()
        if makeret.find("Error") > 0:
            buildFailedList.append(path)
        else:
            #make first
            cmd = "objdump -t " + path + "mytest"
            print("objdump cmd is ",cmd)
            objanalysis = os.popen("objdump -t " + path + "mytest").read()
            for line in objanalysis.splitlines():
                if line.find("*UND*") > 0 and line.find("obotcha") >0:
                    index = line.rfind(" ")
                    function = line[index+1:len(line)]
                    if function in functionMap:
                        #print("i find the function")
                        functionMap[function] = 0

4.待ObotchaTestSuit中所有的测试模块都编译完成,且检测完成,
functionMap中剩余的就是没有被测试到的接口

详细脚本见:

GitHub - wangsun1983/ObotchaTestSuiteObotchaTestSuite/script at master · wangsun1983/ObotchaTestSuite · GitHub

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值