0
0

はじめに

PHP でテストファイルを複数作って、それを一つずつ実行するのがめんどくさかったので、テストファイルを一括で実行できるクラスのメモ。
IntelliJ で実行する前提なので、実行ファイルはないです。
パスの指定は各々気をつけましょう。

継承したクラスのメソッドを全部実行

メソッド実行なのでテスト結果が 1 つにまとめられてしまう。
テストじゃなくて別の使い方とかで応用できそうだなとか思ったり。

<?php

namespace Tests\Controller;

class AllControllerTestExecutor
{
    /**
     * ControllerTestTemplate を継承しているクラスを全て取得し、テストを実行する
     * @return void
     */
    public function testAll(): void
    {
        // ./Api ディレクトリ内の Controller のテストクラスを全て取得する
        $controller_test_classes = $this->getControllerTestClasses();
        foreach ($controller_test_classes as $controller_test_class) {
            // ControllerTestTemplate を継承しているクラスのみテストを実行する
            if (!($controller_test_class instanceof ControllerTestTemplate)) {
                continue;
            }
            // テストを実行する
            $controller_test_class->testAll();
        }
    }

    /**
     * ./Api ディレクトリ内の Controller のテストクラスを全て取得する
     * @return array ./Api ディレクトリ内の Controller のテストクラス
     */
    private function getControllerTestClasses(): array
    {
        $controller_test_classes = [];
        $files = glob(__DIR__ . '/Api/*ControllerTest.php');
        foreach ($files as $file) {
            $class_name = 'Tests\\Controller\\Api\\' . basename($file, '.php');
            $controller_test_classes[] = new $class_name();
        }
        return $controller_test_classes;
    }
}

(おすすめ) フォルダ内にあるテストメソッドを全て実行する

テストメソッドを全て実行するので、それぞれのメソッドごとにテスト結果が出力される。

<?php

namespace Tests\Controller;

use PHPUnit\Framework\TestSuite;

class AllControllerTestSuite
{
    /**
     * ControllerTestTemplate を継承しているクラスを全て取得し、テストを実行する
     * @return TestSuite
     */
    public static function suite(): TestSuite
    {
        $suite = new TestSuite('All Controller Tests');

        $controllerTestClasses = AllControllerTestSuite::getControllerTestClasses();

        foreach ($controllerTestClasses as $controllerTestClass) {
            if (!($controllerTestClass instanceof ControllerTestTemplate)) {
                continue;
            }
            $suite->addTestSuite(get_class($controllerTestClass));
        }

        return $suite;
    }

    /**
     * ./Api ディレクトリ内の Controller のテストクラスを全て取得する
     * @return array ./Api ディレクトリ内の Controller のテストクラス
     */
    private static function getControllerTestClasses(): array
    {
        $controller_test_classes = [];
        $files = glob(__DIR__ . '/Api/*ControllerTest.php');
        foreach ($files as $file) {
            $class_name = 'Tests\\Controller\\Api\\' . basename($file, '.php');
            $controller_test_classes[] = new $class_name();
        }
        return $controller_test_classes;
    }
}

おまけ

コマンドラインでも実行可能。
パスが違ったりすると思うのでそれは頑張ってください (丸投げ)

/bin/php /var/pj/vendor/phpunit --no-configuration --testsuite "/(Tests\\Controller\\AllControllerTestSuite::suite)( .*)?$/" --test-suffix AllControllerTestSuite.php /var/pj/tests/Controller --teamcity --cache-result-file=/var/pj/.phpunit.result.cache --testdox --verbose
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0