Code Coverage – lcov is the answer
So after a lot of mucking around I now have coverage working – the next step in getting my TDD groove together.
It took a while to get to that point, because as soon as I added the flags to the makefile to enable coverage, I found cocos2dx had been compiled up by default for Chipmunk physics and not Box2D, which is what I want to use.
Rebulding with different flags took ages and finally after plenty of googling and a couple of hours of headscratching I forcefully went through a lot of the main source Makefiles and forced it to build a Box2D version. Come on people don’t force the choice and do document it correctly – we aren’t all using Xcode or Eclipse!
To make sure I didn’t lose my changes, I pushed the whole repo up to my bitbucket account. Trouble is now do I also include the project which I’ve kept separate, make it into a submodule, merge tree the stuff. I’ve kept them separate for the moment but I’m sure it will come back to bite me later.
So once that build issue was out of the way and I could compile again the next step was to add the coverage flags to the unit test makefile:-
CXXFLAGS += -fprofile-arcs -ftest-coverage -DCC_ENABLE_BOX2D_INTEGRATION
Then a make and I’ve got all sorts of gcno, gcda and .d files in my object tree.
I’ve recently done a lot of unit tests with phpunit, which has html coverage as an option and I wanted a good visual display of my code coverage to see how I’m doing with my unit tests.
So step 1, try gcov – The documentation was quite terse and rubbish really – examples with sources in the same directory are ok but when your sources are spread across lots of directories running gcov just created lots of files and couldn’t find all the sources. A bit of playing around for 1/2 an hour and I’ve got one .gcov file with source in it.
Thinking I’d have to do this for all source files I thought I’d move on and get some html output somehow. I did a youtube search and after a couple of duffers found someone demoing gcov and then lcov.
That was the clincher lcov does it all – no messing about with gcov first.
I created a script so it would be easy to recreate the coverage.
# Run this in the unit test directory
# reset the counters
lcov -z -d ../obj/debug/
# Run the unit tests to get a coverage count
# Generate the coverage report
lcov -d ../obj/debug/ -b .. --capture --output-file beadbash.info
# Create the html
# open the coverage index in a browser
The result – beautiful. I’ve got 80% coverage for my StartScene.cpp file with only the fail if CCLayer doesn’t init properly and the button callbacks not done yet. I cannot tell you just how cool this is. Also My Bead class has poor coverage which is also excellent as I’ve not even included it in the game scene yet so its not called. Here’s an example