テストの依存関係
テストを実行する前に、特定の前提条件やイベントの発生が必要な場合があります。それがないと成功しません。たとえば、アカウントの確立を確認してからユーザーがアカウントを変更できることを確認できる可能性があります。
この問題に対処するために、Pest では depends()
メソッドが用意されており、`従属テスト` で 1 つ以上の `親テスト` への依存関係を指定できます。
1test('parent', function () {2 expect(true)->toBeTrue();3});4 5test('child', function () {6 expect(false)->toBeFalse();7})->depends('parent');
この例では、parent
テストが正常に完了すると child
テストがトリガーされます。

parent
テストが失敗した場合、child
テストはバイパスされ、テスト結果にわかりやすいメッセージが表示されます。
1test('parent', function () {2 expect(true)->toBeFalse();3});4 5test('child', function () {6 expect(false)->toBeFalse();7})->depends('parent');
上記の例では、次の出力が得られます。

it()
関数は、デフォルトでテストに「it」という接頭辞を付けます。したがって、depends()
メソッドでテスト名を参照するときは、「it」という接頭辞を含める必要があります。
1it('is the parent', function () {2 expect(true)->toBeTrue();3});4 5test('child', function () {6 expect(false)->toBeFalse();7})->depends('it is the parent');
結果は次のようになります。

親テストは、child
テストの引数としてアクセスできる戻り値を提供することさえできます。
1test('parent', function () { 2 expect(true)->toBeTrue(); 3 4 return 'from parent'; 5}); 6 7test('child', function ($parentValue) { 8 var_dump($parentValue); // from parent 9 10 expect($parentValue)->toBe('from parent');11})->depends('parent');
テストに複数の依存関係を追加することも可能です。ただし、すべての親テストに合格する必要があり、各テストによって返される値は、指定された依存関係と同じ順序でファンクションパラメータとして使用できます。
1test('a', function () { 2 expect(true)->toBeTrue(); 3 4 return 'a'; 5}); 6 7test('b', function () { 8 expect(true)->toBeTrue(); 9 10 return 'b';11});12 13test('c', function () {14 expect(true)->toBeTrue();15 16 return 'c';17});18 19test('d', function ($testA, $testC, $testB) {20 var_dump($testA); // a21 var_dump($testB); // b22 var_dump($testC); // c23})->depends('a', 'b', 'c');
テストの依存関係は一般的ではありませんが、テストを最適化してリソースを繰り返し再作成する必要性を最小限に抑えるのに役立ちます。次の章では、プラグインを作成する方法について説明します: プラグインを作成