3

私は次のtests/フォルダーを持っています:

テスト/
    ClassOne.test.php
    ClassTwo.test.php
    ClassThree.test.php

setUp()以下のメソッドとtearDown()メソッドをこれらの各ファイルにコピーする必要があります。

    パブリック関数setUp()
    {{
        $ this-> dbTestData();
    }

    パブリック関数tearDown()
    {{
        $ this-> dbClear();
    }

    プライベート関数dbTestData(){
        //データベースにいくつかのエントリを入力します
        Entities \ AutocompleteValue :: create(array('field_name' =>'testing1'、'entry_value' =>'Aisforapple'));
        Entities \ AutocompleteValue :: create(array('field_name' =>'testing2'、'entry_value' =>'Bisforball'));
        Entities \ AutocompleteValue :: create(array('field_name' =>'testing3'、'entry_value' =>'Cisforcat'));
        Entities \ AutocompleteValue :: create(array('field_name' =>'testing4'、'entry_value' =>'Disfordog'));
    }

    プライベート関数dbClear(){
        DB :: table('autocomplete_values')-> delete();
    }

これらのメソッドを含む単一の個別のクラスを作成することを検討しました。require()そのファイルは各テストファイルにあり、の代わりにそのクラスから拡張されますPHPUnit_Framework_Testcase。これに対するより簡単な解決策はありますか?

phpunit.xml私のコーディングフレームワークのCLIツール(Laravel、職人)がその作成を処理するため、に簡単にアクセスできません。したがって、そのファイルを含まない解決策があるとよいでしょう。

4

1 に答える 1

3

Give in and create the base class. That is the standard solution in this situation. It brings other advantages, such as a good place to keep the static assert extensions you create.

I'm struggling to even come up with an alternative approach, let alone one that is easier. All I can suggest is to use a script that scans each test file and inserts setUp, tearDown and their dependencies if they are not found. But (IMHO) that is a much more complex solution for no significant benefit.

于 2013-02-10T04:10:41.533 に答える