LoginSignup
0
1

More than 5 years have passed since last update.

依存のテストの書き方

Posted at

「"試験A"から生成された"結果R"に対して"試験B"及び"試験C"を実施する」といった場合、テストを分けて書くことができる。
「データプロバイダ テスト」とググれば、複数の郵便番号まとめてテストしたりする方法とかも出てくる。

メリット

  • 一つの testHogehogeが膨らみすぎるのを防げるほか、"試験A"がこけた場合に自動で"試験B/C"をスキップしてくれたりとっても便利

書き方

  • @depends で依存するテストを指定する
  • 依存される"試験A"の方ではreturnを記述する
    • そのreturnを @depends を記述した"試験B/C"の引数で受け取る
  • @depends が依存を示すので、@param は書かなくてもOK。
<?php
namespace Tests\API;

use App\Model\Postalcode;
use Tests\TestCase;

class PostalcodeTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function test郵便番号から都道府県・市区町村を取得(){
        //ランダムな郵便番号をEloquentでとってきて、都道府県・市区町村を取得
        $postalcode = Postalcode::inRandomOrder()->first();
        $response = $this->get("/api/master/postalcode?postalCode=$postalcode->postal_code");
        $content = json_decode($response->baseResponse->content());
        $this->assertTrue(true);
        return [$postalcode, $content];
    }

    /**
     * @param $rtn
     * @depends test郵便番号から都道府県・市区町村を取得
     */
    public function test都道府県から絞った郵便番号との一致($rtn)
    {
        $postalcode = $rtn[0];
        $content = $rtn[1];
        //都道府県からとってきた郵便番号リストに存在するか
        $postalcodefindByPref = Postalcode::where('pref_name', $content->pref)->get(['postal_code']);
        $postalcodeList1 = [];
        foreach ($postalcodefindByPref as $line) {
            $postalcodeList1[] = $line["postal_code"];
        }
        $this->assertTrue(in_array($postalcode->postal_code, $postalcodeList1));
    }

    /**
     * @param $rtn
     * @depends test郵便番号から都道府県・市区町村を取得
     */
    public function test市区町村から絞った郵便番号との一致($rtn)
    {
        $postalcode = $rtn[0];
        $content = $rtn[1];
        //市区町村からとってきた郵便番号リストに存在するか
        $postalcodefindByCity = Postalcode::where('city_name', $content->city)->get(['postal_code']);
        $postalcodeList2 = [];
        foreach ($postalcodefindByCity as $line) {
            $postalcodeList2[] = $line["postal_code"];
        }
        $this->assertTrue(in_array($postalcode->postal_code, $postalcodeList2));
    }

}
0
1
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
1