Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating gcda files to view the code-coverage from XCTests in IOS with Jenkins

I want to view the code coverage by running gcovr on generated gcda files. Jenkins seems to put the generated gcda files in Users/../Library/developer/Xcode/DerivedData/../../../i386. I expect them to be in Users/Shared/Jenkins/workspace/../build/example.build/Debug-iphonesimulator/example.build/Objects-normal/i386

When i run my IOS project with XCode locally it does generate gcda files and i can view my coverage. Im running xcode 5 and all tests are created with XCTest.

I've set 'Generate Test Coverage Files=YES' and 'Instrument Program Flow=YES' for debug and release, basicly i have done everything according to this post

In Jenkins i use the xcode plugin to build. It has two build commands. The first one builds with the target "example" and configuration debug. The second Xcode build command builds with the target "ExampleTests", configuration debug, arguments" test -destination OS=7.0,name=”iPhone Retina (4-inch)” and a scheme . In the output i can see the test being run and the simulator starts on the building machnine.

It seems i have missed something maybe in the project setting or havent set something right in the jenkins job. Maybe something like TEST_AFTER_BUILD=YES only then for XCode5.

like image 614
Staalk Avatar asked Oct 21 '22 20:10

Staalk


1 Answers

As you recognised the .gcda files are put in the "wrong" directory.

Do the following:

  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!

Tell me if something is not working for you or you found a better solution!

like image 175
hirschfl Avatar answered Oct 23 '22 09:10

hirschfl