6
votes

I'm following the ZF2 convention of storing tests in the modules and everything is working fine when tests are run from within each module. What I would like to do is have a root level phpunit.xml that calls the individual module tests and consolidates them to produce code coverage data and other metrics.

The problem is that each individual test suite is bootstrapped within the modular phpunit.xml files. The only way I'm aware of doing things is to configure the bootstrap in each phpunit.xml file which obviously doesn't get picked up when running tests from root as individual xml files are ignored.

So my question is: is there a way for a root-level phpunit.xml file to read individual phpunit.xml and bootstrap files from modules, a kind of phpunit config inheritance if you will? I could go down the route of writing this in Phing or a CI script but I'd like it done quick and dirty while in development and this solution still wouldn't produce a consolidated code report.

Basically, I want something like this, but rather than run the tests, I want it to run the individual phpunit.xml files within each module. Is this possible?

<?xml version="1.0" encoding="UTF-8"?>

<phpunit>
    <testsuites>
        <testsuite name="Site Tests">
            <directory>./module/Application/test/ApplicationTest</directory>
            <directory>./module/User/test/UserTest</directory>
        </testsuite>
    </testsuites>
</phpunit>

Update

The aim is to have code metrics generated by PHPUnit that give a project overview, not a modular specific overview. I appreciate the scripts in the answers will run the unit tests on a per-module basis but this is not what I'm looking for. I understand that this may be a limitation as far as PHPUnit is concerned. I will look into this over the next few days and try and find a solution as it seems like something that would be useful in a lot of projects that deal with custom modules.

Update 2

Robert Basic came up with a script that creates a directory structure with the module reports inside and it works great but would be nice to have it running within PHPUnit with the proper metrics reporting.

https://gist.github.com/robertbasic/5329789

2
I asked a similar question some time ago, but without much luck. I haven't looked at it since, but what I think I will be doing is to use Phing for this. If your IDE does not support it (PhpStorm does, for instance), then it will be as simple as running phing test or similar from the command line after you have made a test target. It's not what you are looking for, but it's a simple workaround that I will probably use myself.ba0708
If you want to use ant then another solution can be found [here][1]. [1]: stackoverflow.com/questions/16628427/…Joseph Cota

2 Answers

1
votes

If you are using Linux you could create a simple script. Not exactly the solution you wanted, but could help none the less:

#!/bin/sh

modDir=$(pwd)
for i in *; do

    if [[ -d $i/test ]]; then
        cd $i/test
        phpunit
        cd $modDir
    fi
done

You could just drop that in to a runtests.sh file in the module directory.

Just a thought :)

1
votes

Having previously discussed this on irc with various people I'm pretty sure you can't, you need to write a script to loop through the modules and run them on a module by module basis.