How can I get PHPUnit to ignore the file template?

I am doing code coverage reports for a project and there are a ton of files that are included or required while running tests that are not actually required for testing or being added to coverage reports (I am using Zend Framework 2; config + Module files are the culprit here).

Is there an easy way to just ignore the file pattern modules/*/Module.php

?

This is my blacklist:

<filter>
    <blacklist>
        <directory>config</directory>
        <directory>vendor</directory>
        <directory>module/*/config</directory>
    </blacklist>
</filter>

      

However, the addition <file>module/*/Module.php</file>

does not affect HTML code coverage reports; adding it does not affect the inclusion of files Module.php

in coverage reports.

Zend Framework is loaded into phpunit file bootstrap.php

using the usual

Application::init(require "config/application.test.php")

      

Except for adding a file Module.php

for each individual module to the blacklist, is there a way PHPUnit can actually do this correctly? I'm not looking for answers that use the method setUp

in PHPUnit test cases; I am looking for a config.

I am using PHPUnit 4.7 + 4.8.

+3


source to share


2 answers


So, to exclude multiple files with the same name <file>

usually does nothing as it will only match exactly one file.

The solution is to use <directory>

but to use the suffix attribute;

<phpunit
    bootstrap="tests/bootstrap.php"
    colors="true"
    convertErrorsToExceptions="true"
    convertNoticesToExceptions="true"
    convertWarningsToExceptions="true">
    <testsuites>
        <testsuite name="Test Suite">
            <!-- functional tests -->
            <directory>tests</directory>
            <!-- unit tests -->
            <directory>module/*/test</directory>
        </testsuite>
    </testsuites>

    <!-- ignore folders for code coverage -->
    <filter>
        <blacklist>
            <directory>config</directory>
            <directory>vendor</directory>
            <directory>module/*/config</directory>
            <directory suffix="Module.php">module/*</directory>
        </blacklist>
    </filter>
</phpunit>

      



<directory suffix="Module.php">module/*</directory>

will match multiple files ending Module.php

in the top level of the module directory, including Module.php

.

Since there is no reason for any other php file to be in this directory in Zend Framework 2, this is probably the best way to do it.

+1


source


Where did you add your file tag?

This is my tag blacklist

which it works for me.

<?xml version="1.0" encoding="UTF-8"?>
<phpunit
    bootstrap="Bootstrap.php"
    colors="true"
    stopOnError="true"
    stopOnFailure="false"
    strict="true"
    verbose="true">
    <testsuites>
        <testsuite name="All Modules">
            <directory>../module/*/tests/*Test</directory>
        </testsuite>
    </testsuites>
   <filter>
        <blacklist>
            <directory suffix=".php">../vendor</directory>
            <directory suffix=".php">../config</directory>
            <directory suffix=".php">../data</directory>
            <directory suffix=".php">../module/*/src/*/Controller</directory>
            <file>../module/*/Module.php</file>
            <file>../module/*/src/*/Factory/*ControllerFactory.php</file>
            <directory suffix=".php">../module/*/tests/*</directory>
        </blacklist>
    </filter>
    <php>
        <ini name="memory_limit" value="2047M" />
    </php>
    <logging>
      <log type="coverage-html" target="../build/tests/coverage/" charset="UTF-8"
           yui="true" highlight="false"
           lowUpperBound="35" highLowerBound="70"/>
      <log type="coverage-clover" target="../build/tests/clover.xml"/>
      <log type="junit" target="../build/tests/junit.xml" logIncompleteSkipped="false"/>
    </logging>
</phpunit>

      



I have this structure:

- module
- vendor
- tests
  - phpunit.xml
- public
- build.xml

      

+2


source







All Articles