Возможный способ ускорения работы PHPUnit тестов

13 августа 2011

Если в файле phpunit.xml директории для тестирования (XPath: /phpunit/filter) указывать через whitelist, а не через blacklist путем запрета тестирования определенных директорий, то можно хорошо ускорить процесс тестирования приложения.

Например вот так будет довольно медленно:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="bootstrap.php" colors="false" convertErrorsToExceptions="true" convertNoticesToExceptions="true"
        convertWarningsToExceptions="true" stopOnFailure="false">
    <selenium>
        <browser name="Firefox" browser="*firefox"/>
        <!--<browser name="Internet Explorer" browser="*iexplore"/>-->
    </selenium>
 
    <filter>
        <blacklist>
            <directory suffix=".php">C:/php</directory>
            <directory suffix=".php">../../common</directory>
            <directory suffix=".php">../../framework</directory>
            <directory suffix=".php">../forms</directory>
            <directory suffix=".php">../tests</directory>
        </blacklist>
    </filter>
 
    <logging>
        <log type="coverage-html" target="./report"/>
    </logging>
</phpunit>

Вывод Ant:

Buildfile: C:\_work\eclipse\anotherproject.kz\build.xml
props:
unit-tests:
     [echo] Running unit tests.
     [exec] PHPUnit 3.5.13 by Sebastian Bergmann.
     [exec] ................................................................. 65 / 82 ( 79%)
     [exec] .................
     [exec] Time: 21 seconds, Memory: 20.75Mb
     [exec] OK (82 tests, 2998 assertions)
     [exec] Generating code coverage report, this may take a moment.
BUILD SUCCESSFUL
Total time: 33 seconds

А вот так уже гораздо быстрее:

<filter>
    <whitelist>
        <directory suffix=".php">../models</directory>
        <directory suffix=".php">../components</directory>
    </whitelist>
</filter>

Вывод Ant:

     [exec] Generating code coverage report, this may take a moment.
BUILD SUCCESSFUL
Total time: 21 seconds

Суть проблемы заключается в том, что в MS Windows PHPUnit сравнительно долго парсит богатые на PHP файлы директории, указанные в blacklist (в моем случае там было указано все, что находилось в include_path, а внутри было много тяжелого). blacklist стоит избегать.