vimfluencer
vimfluencer

Reputation: 3226

PHPUnit - one coverage report from multiple project folders?

I have a project that is structured like so:

project
├── app
│   ├── phpunit.xml
│   ├── src
│   └── tests
│       └── unit
└── modules
    ├── module1
    │   ├── phpunit.xml
    │   ├── src
    │   └── tests
    │       └── unit
    └── module2
        ├── phpunit.xml
        ├── src
        └── tests
            └── unit

All of the core interfaces/classes are in app/src, and all of the extensions/implementations are in modules/**. Thus, the src folders contain all the Controllers, Models, etc., and the adjacent tests/unit folder contains all of the unit tests for these objects.

I'm trying to generate one coverage.xml report (and more importantly an HTML report) that contains the coverage results from all of the tests across both app/ and modules/. Can anyone suggest the best way to accomplish this?

Upvotes: 5

Views: 1455

Answers (2)

Adam
Adam

Reputation: 29109

With phpunit 10 or above you can use the source element. Place this content as your phpunit.xml in your root folder.

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
>
    <testsuites>
        <testsuite name="App">
            <directory>app/tests</directory>
        </testsuite>
        <testsuite name="Modules">
            <directory>modules/**/tests/</directory>
        </testsuite>
    </testsuites>
    <source>
        <include>
            <directory>app</directory>
            <directory>modules</directory>
        </include>
    </source>
</phpunit>

Upvotes: 0

ins0
ins0

Reputation: 3928

You could create one phpunit.xml in your project root folder.

When running PHP Unit just point to your project root and PHPUnit will run over every test files he can find.

The same goes with the generated coverage.xml that now includes all results from every testfile.

If you like to seperate test runs then look at the PHPunit Option testsuite.

Example:

project
├── phpunit.xml
├── app (testsuite app)
└── modules
    ├── module1 (testsuite module2)
    └── module2 (testsuite module1)

Insert into your phpunit.xml

<testsuites>
  <testsuite name="app">
    <directory>app</directory>
  </testsuite>
  <testsuite name="module1">
    <directory>modules/module1</directory>
  </testsuite>
  <testsuite name="module2">
    <directory>modules/module1</directory>
  </testsuite>
</testsuites>

And calling with

phpunit --configuration phpunit.xml --testsuite module1

Upvotes: 5

Related Questions