美文网首页持续集成iOS 自动化专题自动化测试/CI测试
Jenkins实现iOS自动化测试及覆盖率报告输出

Jenkins实现iOS自动化测试及覆盖率报告输出

作者: 廖丹_18be | 来源:发表于2018-05-17 14:28 被阅读24次

    一、Jenkins安装,Xcode插件安装
      略
    二、Xcode工程准备
    Xcode工程(这里我用的是workspace)包含两个Target,一个是UnitTest项目


    image.png

    打开Xcode左上角Manage Schemes,将Shared打钩


    image.png

    选中项目的Scheme点击左下角Edit,打开Gather coverage data,打开覆盖率收集,在Debug模式下会收集覆盖率报告。


    image.png

    写好UnitTestCase,command+u跑一下,在Xcode里可以看到用例结果报告和覆盖率报告。


    image.png

    三、集成到Jenkins实现自动化测试

    新建Job,设置源码branch,这里最好新建一个专门用于测试的branch,这里取名HuaXiaFinance-iOS-test.

    image.png

    设置构建触发器*/5 * * * *,每5分钟检查一次源码变化。


    image.png

    增加构建步骤,选择Execute shell


    image.png

    !/bin/sh -l

    source /etc/profile

    cd ./huaxia-ios/HuaXiaFinance2.0

    xcodebuild test -workspace HuaXiaFinance.xcworkspace -scheme HuaXiaFinanceTests -destination 'platform=iOS Simulator,name=iPhone 6,OS=11.2' -enableCodeCoverage YES 2>&1 | ocunit2junit

    slather coverage --html --input-format profdata --binary-file /Users/huaxiajinrong/Library/Developer/Xcode/DerivedData/HuaXiaFinance-dyxskvcwlazyufhahtutvjhorkwr/Build/Products/Debug-iphonesimulator/HuaXiaFinance.app/HuaXiaFinance --scheme HuaXiaFinanceTests --workspace HuaXiaFinance.xcworkspace --configuration Debug --output-directory reports HuaXiaFinance.xcodeproj

    这里用到两个工具, ocunit2junit 以及slather.

    1、由于Jenkins只接收Junit的单元测试报告,这里要安装一个将脚本执行结果的ocunit格式的测试报告转化为JUnit报告格式的脚本,该项目名叫OCUnit2JUnit,项目地址点这里。安装非常简单,命令行下执行gem install ocunit2junit(可能需要sudo权限)。

    2、代码覆盖率
    xcode test完成后生成的代码覆盖率文件为Coverage.profdata,存放路径/Users/huaxiajinrong/Library/Developer/Xcode/DerivedData/HuaXiaFinance-dyxskvcwlazyufhahtutvjhorkwr/Build/ProfileData/C054A844-6A3C-4CF5-9ED0-D1165EF6C46C/Coverage.profdata

    覆盖率文件用llvm-cov解析,命令如下:
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/llvm-cov report -instr-profile /Users/huaxiajinrong/Library/Developer/Xcode/DerivedData/HuaXiaFinance-dyxskvcwlazyufhahtutvjhorkwr/Build/ProfileData/C054A844-6A3C-4CF5-9ED0-D1165EF6C46C/Coverage.profdata /Users/huaxiajinrong/Library/Developer/Xcode/DerivedData/HuaXiaFinance-dyxskvcwlazyufhahtutvjhorkwr/Build/Products/Debug-iphonesimulator/HuaXiaFinance.app/HuaXiaFinance

    这里采用slather去做覆盖文件可视化报告的生成,你可以把slather当作llvm-cov的一个前端生成工具。
    //https://github.com/SlatherOrg/slather
    安装如下:
    install : sodu gem install slather

    这里用slather的时候一直报错
    命令行如下:
    slather coverage -s --scheme HuaXiaFinanceTests --workspace HuaXiaFinance.xcworkspace HuaXiaFinance.xcodeproj
    报错如下:
    No product binary found in /Users/liaodan/Library/Developer/Xcode/DerivedData/HuaXiaFinance-avwgmcmxqiypmmdhvmglmtwnnzcx/Build/Intermediates.noindex/ProfileData/ED52BCDB-9142-4D42-A7AB-7E37B480F653.
    被这个问题困扰了两天,最后再github上找到了解决办法,参照:
    https://github.com/SlatherOrg/slather/issues/192

    slather coverage --html --input-format profdata --binary-file /Users/huaxiajinrong/Library/Developer/Xcode/DerivedData/HuaXiaFinance-dyxskvcwlazyufhahtutvjhorkwr/Build/Products/Debug-iphonesimulator/HuaXiaFinance.app/HuaXiaFinance --scheme HuaXiaFinanceTests --workspace HuaXiaFinance.xcworkspace --configuration Debug --output-directory reports HuaXiaFinance.xcodeproj

    当然--binary-file 用这种绝对路径的方式指出不太合理,后续有待优化。

    四、读取显示junit和覆盖率html报告
      这里用到两个jenkins插件,jenkins->系统管理-> 管理插件,找到JUnit Plugin和HTML Publisher plugin,安装重启jenkins。
      增加构建后操作,选择Publish Junit test result report,配置xml文件路劲为第三步配置的test-reports/*.xml。

    image.png

    点击立即构建,等待构建完成,返回job主页,可以看到junit测试结果报告和覆盖率的图表了。

    image.png image.png

    相关文章

      网友评论

        本文标题:Jenkins实现iOS自动化测试及覆盖率报告输出

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