How to show PHPUnit test results and test code coverage in TeamCity

First of all there is a useful article about integration between PHP and TeamCity in the documentation to TeamCity 8 called Getting started with PHP. It is worth to read it.

1. How to show PHPUnit test results in TeamCity
First of all we need an ANT task to run PHPUnit tests. Let’s say that test suites description is in the ...tests/phpunit.xml file. And PHPUnit is running by phpunit.bat

<delete dir="c:\inetpub\wwwroot\codecoverage\myproject"/>
<exec executable="phpunit.bat" failonerror="true">
    <arg value="--coverage-html=c:\inetpub\wwwroot\codecoverage\myproject"/>
    <arg value="--configuration"/>
    <arg value="${basedir}/tests/phpunit.xml"/>
</exec>

Then add this task as a new build step to Build Configuration in TeamCity.
TeamCity ant task
Now run tests and watch the results in build log.
TeamCity build log

Now we want to watch our tests results in a dedicated tab in build information page.
TeamCity can receive messages from running tasks. One good man wrote and shared a special wrapper around PHPUnit that sends such messages to TeamCity server.

We need to download the file phpunit-tc.php and place it for example to the C:\TeamCity folder. Does not matter where is it, we just should know this place. The trick is to run PHPUnit through this wrapper. I just changed the last line in the phpunit.bat add there "C:\TeamCity\phpunit-tc.php" right after %PHPBIN%:

if "%PHPBIN%" == "" set PHPBIN=c:\lib\php\php.exe
if not exist "%PHPBIN%" if "%PHP_PEAR_PHP_BIN%" neq "" goto USE_PEAR_PATH
GOTO RUN
:USE_PEAR_PATH
set PHPBIN=%PHP_PEAR_PHP_BIN%
:RUN
"%PHPBIN%" "C:\TeamCity\phpunit-tc.php" %*

phpunit.bat is normally placed in the php main directory. Note that this configuration can disappear after upgrading of the PHPUnit PEAR extension and should be recreated then.
Now let’s start the build again an now watch! we have got a new tab celled Tests with test results.
TeamCity PHPUnit test results

2. How to show test code coverage in TeamCity
We want to go further and observe how much code is covered by unit tests.
In the ant-task we have created there is directive to write a code coverage information to a special folder
<arg value="--coverage-html=c:\inetpub\wwwroot\codecoverage\myproject"/>. TeamCity uses so called build artefacts to show data associated with a build. Build artefacts are in fact files we got as a results of build process. Now we need files with an information about test coverage in a project. To count these files as an artefact we should configure TeamCity to do that. We need to add a mysterious line
c:\inetpub\wwwroot\codecoverage\myproject => coverage.zip
to the Artefatcs paths secton of General settings of a build.
TeamCity repot tabs
Now TeamCity will self archive code coverage files to coverage.zip file and add this file to the list of build artefacts.
After that we have got an artefact archive with html-files show the code coverage inside an archive we can show it as a separated tab in TeamCity CI server.
Go to Administration->Integrations->Report Tabs->Create new report tab and set parameters for the new Code Coverage tab.
TeamCity add new tab

At last we can see a new Code Coverage tab.
TeamCity code coverage tab