Learn how to execute a specific PHPUnit test method directly from the command line, saving development time and streamlining your testing process.
PHPUnit, a popular testing framework for PHP, provides granular control over test execution. This allows you to run specific tests or groups of tests, which is particularly useful when debugging or focusing on specific parts of your codebase. Here's how to run a single test method, a single test class, or a combination of both:
Run a single test method:
phpunit --filter testMethodName TestClass.php
Replace testMethodName
with the actual method name and TestClass.php
with the test file.
Run a single test class:
phpunit path/to/TestClass.php
Replace path/to/TestClass.php
with the actual path to your test file.
Combine class and method filtering:
phpunit --filter testMethodName path/to/TestClass.php
This runs only testMethodName
within TestClass.php
.
This code provides examples of running PHPUnit tests with a sample project structure containing unit and feature tests. It showcases a simple Calculator class and corresponding test cases, along with a UserRegistrationTest class for feature testing. The code demonstrates how to run specific test methods, classes, or combinations using the --filter
option in PHPUnit.
This example demonstrates the different ways to run PHPUnit tests.
Project Structure:
tests/
|__ Unit/
| |__ CalculatorTest.php
|__ Feature/
|__ UserRegistrationTest.php
Calculator.php
Calculator.php:
<?php
class Calculator
{
public function add($a, $b)
{
return $a + $b;
}
public function subtract($a, $b)
{
return $a - $b;
}
}
tests/Unit/CalculatorTest.php:
<?php
use PHPUnit\Framework\TestCase;
class CalculatorTest extends TestCase
{
public function testAdd()
{
$calculator = new Calculator();
$result = $calculator->add(2, 3);
$this->assertEquals(5, $result);
}
public function testSubtract()
{
$calculator = new Calculator();
$result = $calculator->subtract(5, 2);
$this->assertEquals(3, $result);
}
}
tests/Feature/UserRegistrationTest.php:
<?php
use PHPUnit\Framework\TestCase;
class UserRegistrationTest extends TestCase
{
public function testUserCanRegister()
{
// Test logic for user registration
$this->assertTrue(true);
}
}
Running the Tests:
Run a single test method (testAdd in CalculatorTest):
phpunit --filter testAdd tests/Unit/CalculatorTest.php
Run a single test class (CalculatorTest):
phpunit tests/Unit/CalculatorTest.php
Combine class and method filtering (testSubtract in CalculatorTest):
phpunit --filter testSubtract tests/Unit/CalculatorTest.php
These examples demonstrate how to use the --filter
option to target specific tests or test classes in your PHPUnit test suite.
--filter
Syntax: The --filter
option supports more advanced regular expressions for complex test selection. Refer to the PHPUnit documentation for more details: https://phpunit.readthedocs.io/en/9.5/textui.html#command-line-options
--filter
: You can use the @test
annotation in your test method's docblock to mark it as a test. This can be useful for IDEs that might not fully support PHPUnit's --filter
syntax.@group
. You can then run all tests within a specific group, providing another level of granularity.Feature | Command | Description |
---|---|---|
Run a single test method | phpunit --filter testMethodName TestClass.php |
Runs the specified testMethodName within the TestClass.php file. |
Run a single test class | phpunit path/to/TestClass.php |
Executes all test methods within the specified test class file. |
Combine class and method filtering | phpunit --filter testMethodName path/to/TestClass.php |
Runs only the specified testMethodName within the specified TestClass.php file. |
Mastering these techniques allows you to leverage PHPUnit's capabilities for efficient and targeted testing, ultimately leading to more robust and reliable PHP applications.