Nuno Maduro recently introduced PestPHP's ->only()
method for targeted test execution. This sparked an exploration of various PHP test filtering, skipping, and targeting techniques, covering PHPUnit and Pest.
First, Nuno's ->only()
method:
it('returns a successful response', function () { $response = $this->get('/'); $response->assertStatus(200); })->only(); it('another test', function () { // ... })->only();
This selectively runs marked tests. Both PHPUnit and Pest offer broader filtering options.
Test Filtering
Pest provides command-line flags for filtering:
pest --dirty pest --bail pest --filter 'returns a successful response' pest --retry pest --group|--exclude-group pest --todo
PHPUnit uses similar command-line options:
phpunit --filter test_the_application_returns_a_successful_response phpunit --list-groups phpunit --group api phpunit --exclude-group live
Consult the Pest CLI Reference and phpunit --help
for comprehensive options. Tim MacDonald's "Tips to Speed up Your Phpunit Tests" on Laravel News offers further insights.
Test Skipping
Skipping tests is valuable for managing incomplete or broken tests. Pest uses ->todo()
:
it('requires a valid email')->todo();
Running pest --todo
lists these.
PHPUnit uses markTestIncomplete()
:
public function test_the_application_returns_a_successful_response(): void { $this->markTestIncomplete('it requires a valid email'); // ... }
--display-incomplete
details incomplete tests. markTestAsSkipped()
is for skipping tests based on conditions (e.g., platform).
Targeting PHP/OS Versions
PHPUnit uses attributes:
#[RequiresPhp('8.0')] #[RequiresOperatingSystemFamily('Windows')] public function test_windows_only(): void { // ... }
--display-skipped
shows skipped tests.
Pest offers similar functionality:
it('has home', function () { // })->skipOnPhp('>=8.0.0'); it('has home', function () { // })->skipOnWindows();
IDE Integration
IDEs offer shortcuts to run individual tests. The Better PHPUnit VS Code extension supports PHPUnit and Pest. PHPStorm provides extensive test running capabilities. Sublime Text users can leverage the sublime-phpunit plugin.
The above is the detailed content of Running a Single Test, Skipping Tests, and Other Tips and Tricks. For more information, please follow other related articles on the PHP Chinese website!