Setting up Code Coverage on TeamCity 9.1

So recently I was running through all of the potential CI / CD options in regards to looking for something I could use in our Production Environment and Staging Environment for Continuous Integration / Continuous Deployment and decided to look at TeamCity from JetBrains…

It looks pretty good from the off, easy to set up and easy to customise and use (I’ve got it running in a Vagrant box for testing locally, as everything is heh). Anyway, one of the issues I had was getting Code Coverage out of the PHPUnit tests so I can see them in the build. Despite reading a few online tutorials and even contacting JetBrains support, I couldn’t find the issue – and then I got it – none of them mentioned that you need to create a reporting tab in order to do it heh.

This isn’t a complete guide of how to set up TeamCity for PHP but I’ll cover the very basic build steps and config I’ve used to get a repo checked out, composer installed all relevant elements, PHPUnit tests run and code coverage generated…

Setting Up the Build Config

Screen Shot 2016-03-15 at 12.14.38

so this is the basic config set up, name etc etc. As you can see, in the middle I have told TeamCity to basically take the tests/coverage path and convert it into an archive called coverage.zip – this will keep all of my code coverage generated artifacts in one place – just makes it easier.

NB: You do of course need to make sure you are outputting code coverage in your phpunit.xml configuration file, which you can do by adding the following lines somewhere in your XML:

<logging>
    <log type="coverage-html" target="../tests/coverage" lowUpperBound="35" highLowerBound="80"/>
    <log type="coverage-clover" target="../tests/coverage/clover.xml" />
</logging>

Build Settings:

I have a basic 3 step build for this which simply runs composer install, copies a data file I need into the config directory and then runs phpunit:

Screen Shot 2016-03-15 at 12.24.39.pngObviously, you will already have a build set up ready to go, or if not – just repeat what you would do normally when checking out / starting a project.

Setting your Project Settings:

Once you’ve set up your build and artifacts, you need to set up your reporting tab so you can actually see the results.

Go to your project settings, and click on Report Tabs:

Screen Shot 2016-03-15 at 12.20.11

Add a new Build Report tab (you need to do it as a Build Report so the Code Coverage relates to the build you have done). In the resulting window, you need to tell TeamCity where your index.html file is that you want to use, which you do by using the exclamation ( ! ) joiner – which basically tells TeamCity to look in an archive for the file, like this:

Screen Shot 2016-03-15 at 12.22.09

Once you’ve done this and saved it – you can then run your build and once completed you should see a new tab on the build success screen with Code Coverage in it – click it, and away you go…

Screen Shot 2016-03-15 at 12.23.24

Job Done. Hope that helps some people to at least get Code Coverage in there – of course you can add a number of other options in your build as you see fit such as Mess Detector, Code Sniffer, Phing etc etc.

Advertisements

One thought on “Setting up Code Coverage on TeamCity 9.1

  1. Pingback: TeamCity Digest #5 | TeamCity Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s