PHPUnit fixtures initialize fresh objects and controlled state before each test, keeping outcomes independent of execution order.
What It Does
Fixtures belong in setUp when each test needs a fresh starting state. Use predictable fixture collection sizes. Use expectException before calling the operation expected to fail. Use predictable fixture collection sizes.
Requirements and Compatibility
Use a supported PHP runtime and install any third-party tool or extension explicitly shown in the examples. Commands should be run from the project root unless stated otherwise.
Syntax, Inputs and Results
The examples separate configuration, the smallest working operation and a realistic follow-up task. Adapt file paths and dependency versions to your own project.
Example 1: Basic Use
Fixtures belong in setUp when each test needs a fresh starting state. Use predictable fixture collection sizes.
<?php
use PHPUnit\Framework\TestCase;
final class InvoiceTest extends TestCase {
private array $lines;
protected function setUp(): void { $this->lines = [120, 80]; }
public function testInvoiceSum(): void { self::assertSame(200, array_sum($this->lines)); }
public function test1ArticleBoundary(): void { self::assertSame(3, count([2, 3, 5])); }
}
Example 2: Another Case
Use expectException before calling the operation expected to fail. Use predictable fixture collection sizes.
<?php
use PHPUnit\Framework\TestCase;
final class QuantityTest extends TestCase {
public function testNegativeQuantityIsRejected(): void {
$validate = static function (int $n): void {
if ($n < 0) throw new InvalidArgumentException('Negative quantity');
};
$this->expectException(InvalidArgumentException::class);
$validate(-2);
}
public function test2ArticleBoundary(): void { self::assertSame(3, count([2, 3, 5])); }
}
Practical Example
Keep the test runner in development dependencies and run the project-local executable.
composer require --dev phpunit/phpunit
vendor/bin/phpunit --testsuite unit
For production, apply the compatibility and edge-case limits described below.
Important Behavior and Edge Cases
Separate isolated unit tests from integration tests requiring a real database or network.
Comparison with Related Functions and Tools
| Subject | Key difference |
|---|---|
| Unit tests vs integration tests | Unit tests isolate one behavior; integration tests use actual interfaces, databases or framework components. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Writing tests without meaningful assertions | Assert an exact result or expected exception, including at least one boundary case. |
| Skipping the prerequisite of this specific workflow | Install or configure the named tool and confirm the example can actually run. |
| Ignoring edge conditions | Separate isolated unit tests from integration tests requiring a real database or network. |
Key Takeaways
- PHPUnit fixtures initialize fresh objects and controlled state before each test, keeping outcomes independent of execution order.
- Separate isolated unit tests from integration tests requiring a real database or network.
- Keep application configuration, source code and validation tests under version control.
Use these examples as a starting point, then validate the result with the actual PHP build, dependencies and inputs used by your application.