5

DB関連の単体テストに使用されるテストDBを作成し、テストが完了するとDBを破棄するシャットダウン関数を登録するPHPUnitブートストラップファイルがあります。実行ごとに新しいデータベース!

問題:テストが失敗した場合、デバッグのためにデータベースを保持したいと思います。現在、手動で呼び出しを無効にしてからregister_shutdown_function()、テストを再実行する必要があります。

実行のためにPHPUnitの最終的な成功/失敗状態にアクセスできれば、PHPUnitブートストラップファイル内のスイッチに基づいてデータベース破棄プロセスを動的にトリガーできます。

OKPHPUnitは、適切な結果イベント、つまり出力とをトリガーするために、この情報をどこかに格納しますFAILURES!。ただし、私が明らかにしたことから、この情報はユーザーレベルのブートストラップファイルに公開されていません。誰かがこのようなことをしたことがありますか?

探索したい場合は、コマンドラインからPHPUnitを実行したときに発生するPHPUnitのスタックトレースを次に示します...

PHP   1. {main}() /usr/bin/phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP   3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:130
PHP   4. PHPUnit_TextUI_Command->handleArguments() /usr/share/php/PHPUnit/TextUI/Command.php:139
PHP   5. PHPUnit_TextUI_Command->handleBootstrap() /usr/share/php/PHPUnit/TextUI/Command.php:620
PHP   6. PHPUnit_Util_Fileloader::checkAndLoad() /usr/share/php/PHPUnit/TextUI/Command.php:867
PHP   7. PHPUnit_Util_Fileloader::load() /usr/share/php/PHPUnit/Util/Fileloader.php:79
PHP   8. include_once() /usr/share/php/PHPUnit/Util/Fileloader.php:95
PHP   9. [YOUR PHPUNIT BOOTSTRAP RUNS HERE]
4

1 に答える 1

6

PHPUnitテストケースの状態にアクセスするには、通常、以下を使用することをお勧めします。

docs:PHPUnit_Framework_TestListenerそれをxmlconfigに追加する方法を参照してください。

小さなサンプル:

これでXMLファイルを拡張する

<listeners>
 <listener class="DbSetupListener" file="/optional/path/to/DbSetupListener.php"/>
</listeners>

サンプルリスナー

<?php
class DbSetupListener implements PHPUnit_Framework_TestListener {
    private $setupHappend = false;

    public function addError(PHPUnit_Framework_Test $test, Exception $e, $time) {
        $this->error = true;
    }

    public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) {
        $this->error = true;
    }

    public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time) { }
    public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time) { }
    public function startTest(PHPUnit_Framework_Test $test) { }
    public function endTest(PHPUnit_Framework_Test $test, $time) { }

    public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
    {
        if(!$this->setupHappend) { 
            $this->setupDatabase();
            $this->setupHappend = true;
        }
    }

    public function endTestSuite(PHPUnit_Framework_TestSuite $suite) {
        // If you have multiple test suites this is the wrong place to do anything
    }

    public function __destruct() {
        if($this->error) {
            // Something bad happend. Debug dump or whatever
        } else {
            // Teardown
        }
    }

}

これでかなり簡単になります。tests特定のまたはを「リッスン」するだけでよい場合は、およびtestsuitesのパラメータを使用できます。startTeststartTestSuite

両方のオブジェクトには、getName()それぞれテストスイートとテストケースの名前を与えるメソッドがあります。

于 2012-09-27T02:37:16.580 に答える