phpunitの実行時のオプションはいろいろあります。今回は私が便利!と思ったオプションを紹介します。

まず、testsのディレクトリの構造が以下と仮定します。

tests
├── CreatesApplication.php
├── Feature
│   └── ExampleTest.php
├── TestCase.php
└── Unit
    ├── ExampleTest.php
    └── UserTest.php

オプションなしでphpuitを実行すると、tests/Unit, tests/Featureのディレクトリのファイル名ががTest.phpで終わるファイルに含まれるテストすべてを実行します。

$ vendor/bin/phpunit                          
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

...                                                                 3 / 3 (100%)

Time: 621 ms, Memory: 24.00 MB

OK (3 tests, 3 assertions)

どのテストが実行されるかは、--list-testsのオプションで出力してくれます。

$ vendor/bin/phpunit --list-tests
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

Available test(s):
 - Tests\Unit\ExampleTest::testBasicTest
 - Tests\Unit\UserTest::test_create_a_user
 - Tests\Feature\ExampleTest::testBasicTest

実行しながら、どのテストが実行されるのを見たいなら、--testdoxのオプションを付けます。
注意:以下のそれぞれのテストでのxは成功という意味で、実際には✔で表示されます。コードのセクションではエスケープされるので置き換えています。

$ vendor/bin/phpunit --testdox       
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

Example (Tests\Unit\Example)
 x Basic test

User (Tests\Unit\User)
 x Create a user

Example (Tests\Feature\Example)
 x Basic test

Time: 582 ms, Memory: 24.00 MB

OK (3 tests, 3 assertions)

特定のファイルに含まれるテストのみを実行したいなら、以下のようにファイル名を指定します。

$ vendor/bin/phpunit tests/Feature/ExampleTest.php

あるいは、テスト名のみでもOKです。

$ vendor/bin/phpunit tests/Feature/ExampleTest

実行したいテストをパターンで指定することも可能です。

例えば、Exampleから始まるテストファイルだけを実行したいなら、以下のように、--filterオプションを使用します。

$ vendor/bin/phpunit --filter Example --testdox   
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

Example (Tests\Unit\Example)
 x Basic test

Example (Tests\Feature\Example)
 x Basic test

Time: 323 ms, Memory: 16.00 MB

OK (2 tests, 2 assertions)

Exampleで始まるテストファイル名は、UnitFeatureに存在するので両方が実行されます。

テストファイルに含まれる、テスト名の指定も可能です。

$ vendor/bin/phpunit --filter testBasicTest --testdox
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

Example (Tests\Unit\Example)
 x Basic test

Example (Tests\Feature\Example)
 x Basic test

Time: 304 ms, Memory: 16.00 MB

OK (2 tests, 2 assertions)

testBasicTestのテストは、これまたUnitFeatureのExampleTest.phpに存在するので両方が実行されます。

特定のファイルの特定のテストだけを実行したいなら、以下のようにテスト名とパス名を指定します。

$ vendor/bin/phpunit --filter testBasicTest tests/Unit/ExampleTest.php --testdox
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

Example (Tests\Unit\Example)
 x Basic test

Time: 148 ms, Memory: 6.00 MB

OK (1 test, 1 assertion)

オプションなしでのphpunitの実行は、phpunit.xmlのtestsuitesの編集で変更することが可能です。以下はデフォルトの設定です。

...
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>

        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
..
</phpunit>

現在2つのtestsuiteが存在しますが、Unitの方だけを実行したいなら、

$ vendor/bin/phpunit --testsuite Unit --testdox
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

Example (Tests\Unit\Example)
 x Basic test

User (Tests\Unit\User)
 x Create a user

Time: 523 ms, Memory: 22.00 MB

OK (2 tests, 2 assertions)

と指定できます。

現在、私のあるプロジェクトではdbunitを使用するテストからRefreshDatabaseを使用するテストに書き換えています。新旧のテストを別々にテストしたいために、phpunit.xmlは以下のように4つのtestsuiteが存在します。

    <testsuites>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
            <exclude>./tests/Feature/Http</exclude>
        </testsuite>
        <testsuite name="Http">
            <directory suffix="Test.php">./tests/Feature/Http</directory>
        </testsuite>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
            <exclude>./tests/Unit/Models</exclude>
        </testsuite>
        <testsuite name="Models">
            <directory suffix="Test.php">./tests/Unit/Models</directory>
        </testsuite>
    </testsuites>

excludeのタグを使用することで、サブディレクトリのテストを非実行とできます。

実行は以下のように2つ実行します。

$ vendor/bin/phpunit --testsuite Feature,Unit
$ vendor/bin/phpunit --testsuite Models,Http

By khino