在iOS中使用Jenkins生成gcda文件以查看XCTests的代码覆盖率

5

我想通过在生成的gcda文件上运行gcovr来查看代码覆盖率。Jenkins似乎将生成的gcda文件放在Users / .. / Library / developer / Xcode / DerivedData / .. / ../i386中。我希望它们在Users / Shared / Jenkins / workspace / .. / build / example.build / Debug-iphonesimulator / example.build / Objects-normal / i386中。

当我在XCode本地运行我的IOS项目时,确实会生成gcda文件,我可以查看我的覆盖率。 我正在运行xcode 5,所有测试都是使用XCTest创建的。

我已经设置了“Generate Test Coverage Files = YES”和“Instrument Program Flow = YES”用于调试和发布,基本上我已经按照this post做了一切。

在Jenkins中,我使用xcode插件进行构建。它有两个构建命令。第一个使用目标“example”和配置调试进行构建。 第二个Xcode构建命令使用目标“ExampleTests”,配置调试,参数“test -destination OS = 7.0,name =”iPhone Retina(4-inch)“和一个方案。在输出中,我可以看到测试正在运行,模拟器在构建机器上启动。

看起来我可能在项目设置中遗漏了一些东西,或者在Jenkins工作中没有设置正确。也许像TEST_AFTER_BUILD=YES这样的东西只适用于XCode5。


我处于同样的情况。当我在构建机器上运行测试构建步骤时,找不到任何.gcda文件。但是我可以在我的机器上找到它们。我认为这些文件是在派生数据文件夹中生成的,而不是在Jenkins的工作区中。 - Mark
1个回答

3

正如您所认识到的,.gcda文件被放在了“错误”的目录中。

请按照以下步骤操作:

  1. Select your application target in your Xcode-Project and go to "Editor -> Add Build Phase -> Add Run Script Build Phase"
  2. Paste this script into the script field:

    echo "Creating derivedDataDirectory file"
    echo "${OBJECT_FILE_DIR_normal}/${CURRENT_ARCH}" > ${PROJECT_DIR}/derivedDataDirectory
    

    XCode setup

    (This creates a file with the path to the derivedDataDirectory)

  3. Go to your jenkins project, click on "Add build step" and select "Execute shell".

  4. Paste this script into the "Command" field:

    #CopyCodeCoverageFile
    echo "Start copying code coverage Files"
    
    projectname="[YOUR PROJECTNAME]"
    
    source=$(cat ${WORKSPACE}/$projectname/derivedDataDirectory)
    
    cp -a $source/. ${WORKSPACE}/$projectname/
    
    
    #CodeCoverage
    echo "Start CodeCoverage"
    
    cd ${WORKSPACE}/$projectname
    
    [YOUR PATH TO GCOVR]/gcovr -r /private/tmp/workspace/${JOB_NAME}/$projectname --xml > ${WORKSPACE}/$projectname/test-reports/coverage.xml
    

    Insert [YOUR PROJECTNAME] and [YOUR PATH TO GCOVR]. If your PROJECTNAME or target has spaces in it, this will cause trouble! Make sure all the paths are correct!

  5. This works for me and if all your paths are correct it should work for you to. Im running XCTests on Jenkins using XCode 5. This should be the same as your setup. If you dont use gcovr to generate code coverage for cobertura you can delete the last step in the script. If you are experiencing problems with gvovr try using gcovr 3.0 instead of 3.1!

如果有什么不适用或者您找到了更好的解决方案,请告诉我!


我已经有一段时间没有发布问题了,目前我无法测试它。逻辑很清晰,我会相信你的话的 ;) - Staalk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接