How to consolidate ZF2 unit/application module tes

2019-06-20 07:01发布

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条回答
Root(大扎)
2楼-- · 2019-06-20 07:50

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.

查看更多
ら.Afraid
3楼-- · 2019-06-20 07:56

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 :)

查看更多
登录 后发表回答