tayguara / paratest

PHP 的并行测试


README

Build Status HHVM Status Scrutinizer Code Quality Packagist

ParaTest 的目标是支持 PHPUnit 的并行测试。

安装

Composer

使用 composer 安装,请将以下内容添加到您的 composer.json 文件中

"require": {
    "brianium/paratest": "dev-master"
}

然后,运行 php composer.phar install

使用方法

安装后,二进制文件可以在 vendor/bin/paratest 中找到。使用方法如下

Usage:
 paratest [-p|--processes="..."] [-f|--functional] [--no-test-tokens] [-h|--help] [--coverage-clover="..."] [--coverage-html="..."] [--coverage-php="..."] [-m|--max-batch-size="..."] [--filter="..."] [--phpunit="..."] [--runner="..."] [--bootstrap="..."] [-c|--configuration="..."] [-g|--group="..."] [--exclude-group="..."] [--stop-on-failure] [--log-junit="..."] [--colors] [--testsuite[="..."]] [--path="..."] [path]

Arguments:
 path                  The path to a directory or file containing tests. (default: current directory)

Options:
 --processes (-p)      The number of test processes to run. (default: 5)
 --functional (-f)     Run methods instead of suites in separate processes.
 --no-test-tokens      Disable TEST_TOKEN environment variables. (default: variable is set)
 --help (-h)           Display this help message.
 --coverage-clover     Generate code coverage report in Clover XML format.
 --coverage-html       Generate code coverage report in HTML format.
 --coverage-php        Serialize PHP_CodeCoverage object to file.
 --max-batch-size (-m) Max batch size (only for functional mode). (default: 0)
 --filter              Filter (only for functional mode).
 --phpunit             The PHPUnit binary to execute. (default: vendor/bin/phpunit)
 --runner              Runner or WrapperRunner. (default: Runner)
 --bootstrap           The bootstrap file to be used by PHPUnit.
 --configuration (-c)  The PHPUnit configuration file to use.
 --group (-g)          Only runs tests from the specified group(s).
 --exclude-group       Don't run tests from the specified group(s).
 --stop-on-failure     Don't start any more processes after a failure.
 --log-junit           Log test execution in JUnit XML format to file.
 --colors              Displays a colored bar as a test result.
 --testsuite           Filter which testsuite to run
 --path                An alias for the path argument.

优化速度

要充分利用 paratest,您需要仔细调整参数。

  1. 使用 -p 调整进程数

    为了充分利用您的 CPU 核心,您应该为每个核心至少有一个进程。更多的进程允许更好的资源利用,但请记住,每个进程都有自己的启动成本。

  2. 使用 -f 选择按测试用例或按测试方法并行化

    如果您有少量测试用例(类)和许多运行时间长的测试方法,您应该使用 -f 选项启用 功能模式,允许同一类的不同方法并行执行。请记住,默认情况下是按测试用例并行化,以解决测试方法之间的依赖关系。

  3. 如果可能,请使用 WrapperRunner

    PHPUnit 的默认运行器为每个测试用例(或功能模式中的方法)启动一个新的进程。这提供了最高的兼容性,但代价是产生许多进程以及每个进程的引导。特别是当您的测试(如数据库设置)中有慢速引导时,您应该尝试使用 --runner WrapperRunner 的 WrapperRunner。它为每个并行进程(-p)启动一个“工作”进程,执行一次引导并重新使用这些进程执行每个测试。这样,进程启动和引导的 overhead 就降至最低。

  4. 调整最大批处理大小 --max-batch-size.

    批处理大小将影响单个测试方法使用的最大原子测试数量。一个原子测试将是一个测试类中的测试方法(如果没有为方法提供数据提供者)或仅数据提供者中的一个项。增加此值以减少每个进程的 overhead,在大多数情况下也会降低并行效率。减少此值以增加每个进程的 overhead,在大多数情况下也会提高并行效率。如果所有测试的总数小于最大批处理大小,则所有内容将在一个进程线程中处理,因此在这种情况下,paratest 完全无用。找到最有效的批处理大小的最佳方法是在不同的批处理大小值上进行测试,并选择最佳值。最大批处理大小 = 0 表示不会使用批处理分组,一个批处理将等于所有方法测试(一个或所有来自数据提供者)。最大批处理大小 = 1 表示每个批处理将仅包含数据提供者中的一个测试或未使用数据提供者时的一个方法。更大的最大批处理大小可以显着增加 phpunit 命令行长度,因此进程可能会失败。减少最大批处理大小以缩短命令行长度。Windows 的限制约为 32k,Linux - 2048k,Mac OS X - 256k。

Windows

Windows 用户请务必使用适当的批处理文件。

例如

vendor\bin\paratest.bat --phpunit vendor\bin\phpunit.bat ...

ParaTest 假设 PSR-0 用于加载测试。

为了方便起见,ParTest 的 Windows 版本使用 79 列模式,以防止在标准的 80x25 Windows 控制台中出现空白行。

PHPUnit Xml 配置支持

当运行PHPUnit测试时,ParaTest会自动通过--configuration开关将phpunit.xml或phpunit.xml.dist传递给phpunit运行器。ParaTest还允许手动指定配置路径。

ParaTest将依赖于phpunit的xml配置中的testsuites节点来处理套件的加载。

以下phpunit配置文件用于ParaTest的测试用例。

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="../bootstrap.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false"
         syntaxCheck="false"
        >
    <testsuites>
        <testsuite name="ParaTest Fixtures">
            <directory>./tests/</directory>
        </testsuite>
    </testsuites>
</phpunit>

测试令牌

TEST_TOKEN环境变量保证具有与当前运行的其他每个测试不同的值。这对于例如为每个测试使用不同的数据库很有用。

if (getenv('TEST_TOKEN') !== false) {  // Using partest
    $dbname = 'testdb_' . getenv('TEST_TOKEN');
} else {
    $dbname = 'testdb';
}

运行测试

ParaTest的测试套件依赖于通过composer安装的PHPUnit。确保在克隆后运行composer install

注意:必须将php.ini中的display_errors指令设置为stderr才能运行测试套件。

要运行单元测试:vendor/bin/phpunit test/unit

要运行功能测试:vendor/bin/phpunit test/functional

您可以通过在项目目录中运行phpunit来一次性运行所有测试。vendor/bin/phpunit

可以从bin目录运行ParaTest来自行运行其测试套件。bin/paratest

要查看ParaTest的实际应用示例,请查看示例