Home > Backend Development > PHP Tutorial > Running a Single Test, Skipping Tests, and Other Tips and Tricks

Running a Single Test, Skipping Tests, and Other Tips and Tricks

Karen Carpenter
Release: 2025-03-07 00:26:09
Original
844 people have browsed it

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();
Copy after login

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
Copy after login

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
Copy after login

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();
Copy after login

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');
    // ...
}
Copy after login

--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 {
    // ...
}
Copy after login

--display-skipped shows skipped tests.

Pest offers similar functionality:

it('has home', function () {
    //
})->skipOnPhp('>=8.0.0');

it('has home', function () {
    //
})->skipOnWindows();
Copy after login

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.

Running a Single Test, Skipping Tests, and Other Tips and Tricks Running a Single Test, Skipping Tests, and Other Tips and Tricks Running a Single Test, Skipping Tests, and Other Tips and Tricks Running a Single Test, Skipping Tests, and Other Tips and Tricks

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template