| title | Datasets |
|---|---|
| description | Datasets |
Datasets in Pest allows you to run the same test multiple times with different data. Also, the test description will contain information about the arguments used on each test.
This feature is most commonly known as Data Providers in PHPUnit.
Now, in this section, we are going to cover the way you can create datasets in Pest.
An inline dataset may be used for a single test only:
it('has emails', function ($email) {
expect($email)->not->toBeEmpty();
})->with([
'enunomaduro@gmail.com',
'other@example.com'
]);Of course, you can also give multiple arguments providing an array of arguments:
it('has emails', function ($name, $email) {
expect($email)->not->toBeEmpty();
})->with([
['Nuno', 'enunomaduro@gmail.com'],
['Other', 'other@example.com']
]);If you want to keep your test files clean, you can use
the tests/Datasets folder to place your datasets:
tests
- Unit/ComponentTest.php
- Feature/HomeTest.php
- Datasets/Emails.php <--
phpunit.xmlInside this Emails.php you just have to use the dataset() function:
<?php
dataset('emails', [
'enunomaduro@gmail.com',
'other@example.com'
]);In your test, you can use the dataset name to reuse your dataset:
it('has emails', function ($email) {
expect($email)->not->toBeEmpty();
})->with('emails');In both inline and external approaches, you may need to serve your data with PHP's generators to allow you to work with very large datasets while keeping memory usage low:
dataset('emails', function () {
yield 'enunomaduro@gmail.com';
yield 'other@example.com';
});
it('has emails', function ($email) {
expect($email)->not->toBeEmpty();
})->with(function () {
yield 'enunomaduro@gmail.com';
yield 'other@example.com';
});Next section: Coverage →