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
<exec executable="phpunit.bat" failonerror="true">
Then add this task as a new build step to Build Configuration in TeamCity.
Now run tests and watch the results in 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
if "%PHPBIN%" == "" set PHPBIN=c:\lib\php\php.exe
if not exist "%PHPBIN%" if "%PHP_PEAR_PHP_BIN%" neq "" goto USE_PEAR_PATH
"%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.
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.
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.
At last we can see a new Code Coverage tab.