Test Attribute
The #[Test] attribute explicitly marks a method, function, or class as a test.
Can be placed on:
- Class — all public methods with
voidorneverreturn type become tests - Method — only that method is a test
- Function — the function is a test
php
// tests/Unit/Order.php
#[Test]
final class Order
{
public function createsOrder(): void { /* ... */ }
public function calculatesTotal(): void { /* ... */ }
}php
// tests/Unit/Order.php
final class Order
{
#[Test]
public function createsOrder(): void { /* ... */ }
#[Test]
public function calculatesTotal(): void { /* ... */ }
}php
// tests/Unit/order.php
#[Test]
function creates_order(): void { /* ... */ }
#[Test]
function calculates_total(): void { /* ... */ }
#[Test]
function applies_discount(): void { /* ... */ }When to Use
Use #[Test] when:
- You want explicit test declaration without relying on naming patterns
- Your method/function name doesn't follow the
testprefix convention - You prefer attribute-based discovery over convention-based