資料集
使用資料集,您可以定義一個測試資料陣列,而 Pest 會自動為每個設定執行相同的測試。這透過消除以不同資料手動重複相同測試的需求,來節省時間和精力。
1it('has emails', function (string $email) {2 expect($email)->not->toBeEmpty();3})->with(['enunomaduro@gmail.com', 'other@example.com']);
在執行測試時,Pest 會自動為使用資料集的測試加入有用的測試描述,概述每個測試中使用的參數,有助於了解資料並在測試失敗時找出問題。

當然,提供包含引數陣列的陣列就可以提供多個引數。
1it('has emails', function (string $name, string $email) {2 expect($email)->not->toBeEmpty();3})->with([4 ['Nuno', 'enunomaduro@gmail.com'],5 ['Other', 'other@example.com']6]);
若要手動將您自己的描述新增到資料集值,您可以簡單地為其指定一個金鑰。
1it('has emails', function (string $email) {2 expect($email)->not->toBeEmpty();3})->with([4 'james' => 'james@laravel.com',5 'taylor' => 'taylor@laravel.com',6]);
如果新增了金鑰,Pest 會在為測試產生描述時使用該金鑰。

重要的是,在資料集中使用 閉包
時,您必須在傳遞給測試函式的閉包中宣告引數類型。
1it('can sum', function (int $a, int $b, int $result) {2 expect(sum($a, $b))->toBe($result);3})->with([4 'positive numbers' => [1, 2, 3],5 'negative numbers' => [-1, -2, -3],6 'using closure' => [fn () => 1, 2, 3],7]);
繫結資料集
Pest 的繫結資料集可於測試的 beforeEach()
方法之後取得解析後的資料集。這在 Laravel 應用程式 (或任何其他 Pest 整合) 中特別有用,您可能需要在 beforeEach()
方法準備好資料庫架構後建立的 App\Models\User
模型的資料集。
1it('can generate the full name of a user', function (User $user) {2 expect($user->full_name)->toBe("{$user->first_name} {$user->last_name}");3})->with([4 fn() => User::factory()->create(['first_name' => 'Nuno', 'last_name' => 'Maduro']),5 fn() => User::factory()->create(['first_name' => 'Luke', 'last_name' => 'Downing']),6 fn() => User::factory()->create(['first_name' => 'Freek', 'last_name' => 'Van Der Herten']),7]);
如果您願意,可以繫結單一引數至測試案例。但是,Pest 要求它必須在 it|test
函式引數中輸入完整類型。
1-it('can generate the full name of a user', function ($user, $fullName) {2+it('can generate the full name of a user', function (User $user, $fullName) {3 expect($user->full_name)->toBe($fullName);4})->with([5 [fn() => User::factory()->create(['first_name' => 'Nuno', 'last_name' => 'Maduro']), 'Nuno Maduro'],6 [fn() => User::factory()->create(['first_name' => 'Luke', 'last_name' => 'Downing']), 'Luke Downing'],7 [fn() => User::factory()->create(['first_name' => 'Freek', 'last_name' => 'Van Der Herten']), 'Freek Van Der Herten'],8]);
分享資料集
將資料集個別儲存在 tests/Datasets
資料夾中,您可以輕鬆地將它們與測試程式碼區分開來,並確保它們不會讓您的主測試檔案變得雜亂不堪。
1// tests/Unit/ExampleTest.php... 2it('has emails', function (string $email) { 3 expect($email)->not->toBeEmpty(); 4-})->with(['enunomaduro@gmail.com', 'other@example.com']); 5+})->with('emails'); 6 7// tests/Datasets/Emails.php... 8+dataset('emails', [ 9+ 'enunomaduro@gmail.com',10+ 'other@example.com'11+]);
適用於內聯資料集的繫結資料集、描述金鑰和其他規則也可以應用於共用資料集。
範圍資料集
偶爾,資料集可能僅與特定功能或資料夾組有關。在這種情況下,而不是在 Datasets
資料夾中全域散發資料集,您可以在需要資料集的相關資料夾中產生一個 Datasets.php
檔案,並將資料集的範圍僅限制於那個資料夾。
1// tests/Feature/Products/ExampleTest.php... 2it('has products', function (string $product) { 3 expect($product)->not->toBeEmpty(); 4})->with('products'); 5 6// tests/Feature/Products/Datasets.php... 7dataset('products', [ 8 'egg', 9 'milk'10]);
組合資料集
結合內嵌和共用資料集,你可以輕鬆地取得複雜的資料集。這樣做的時候,這些資料集會用笛卡兒乘積方法結合。
在下列範例中,我們驗證在提供的每個星期幾中,所有指定的營業場所都關閉。
1dataset('days_of_the_week', [ 2 'Saturday', 3 'Sunday', 4]); 5 6test('business is closed on day', function(string $business, string $day) { 7 expect(new $business)->isClosed($day)->toBeTrue(); 8})->with([ 9 Office::class,10 Bank::class,11 School::class12])->with('days_of_the_week');
執行上述範例時,Pest 的輸出會包含每組已驗證組合的說明。

重複測試
在某些情況下,你可能需要為了除錯目的或確保測試穩定性而重複測試多次。在這些情況下,你可以使用 repeat()
方法重複進行測試,指定次數。
1it('can repeat a test', function () {2 $result = /** Some code that may be unstable */;3 4 expect($result)->toBeTrue();5})->repeat(100); // Repeat the test 100 times
熟練運用資料集進行測試後,下一個重要的步驟是了解如何對例外狀況進行測試。這涉及驗證當程式碼遇到意外或錯誤輸入時是否能正確執行並擲出適當的例外狀況:例外狀況 →