文档
Hello World:PHPUnit 第一个测试
目标
学习 PHPUnit 基础:创建测试类、编写断言、运行测试、理解测试结果。
完整代码
1. 项目结构
math-app/
├── src/
│ └── Calculator.php
├── tests/
│ └── CalculatorTest.php
├── phpunit.xml
└── composer.json
2. src/Calculator.php
<?php
namespace App;
class Calculator
{
public function add(float $a, float $b): float
{
return $a + $b;
}
public function subtract(float $a, float $b): float
{
return $a - $b;
}
public function multiply(float $a, float $b): float
{
return $a * $b;
}
public function divide(float $a, float $b): float
{
if ($b === 0.0) {
throw new \InvalidArgumentException('Division by zero');
}
return $a / $b;
}
public function fibonacci(int $n): array
{
if ($n < 0) {
throw new \InvalidArgumentException('Negative input');
}
$sequence = [0, 1];
for ($i = 2; $i <= $n; $i++) {
$sequence[] = $sequence[$i - 1] + $sequence[$i - 2];
}
return array_slice($sequence, 0, $n + 1);
}
}
3. tests/CalculatorTest.php
<?php
namespace App\Tests;
use App\Calculator;
use PHPUnit\Framework\TestCase;
class CalculatorTest extends TestCase
{
private Calculator $calculator;
protected function setUp(): void
{
$this->calculator = new Calculator();
}
public function testAdd(): void
{
$this->assertSame(4.0, $this->calculator->add(2.0, 2.0));
$this->assertSame(0.0, $this->calculator->add(-1.0, 1.0));
}
public function testSubtract(): void
{
$this->assertSame(3.0, $this->calculator->subtract(5.0, 2.0));
}
public function testMultiply(): void
{
$this->assertSame(6.0, $this->calculator->multiply(2.0, 3.0));
}
public function testDivide(): void
{
$this->assertSame(2.5, $this->calculator->divide(5.0, 2.0));
}
public function testDivideByZeroThrowsException(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Division by zero');
$this->calculator->divide(5.0, 0.0);
}
#[DataProvider('fibonacciProvider')]
public function testFibonacci(int $input, array $expected): void
{
$this->assertSame($expected, $this->calculator->fibonacci($input));
}
public static function fibonacciProvider(): array
{
return [
'zero' => [0, [0]],
'one' => [1, [0, 1]],
'five' => [5, [0, 1, 1, 2, 3, 5]],
'ten' => [10, [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]],
];
}
public function testFibonacciNegativeThrowsException(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->calculator->fibonacci(-1);
}
}
运行步骤
./vendor/bin/phpunit
# 或带详细输出
./vendor/bin/phpunit --testdox
预期输出
PHPUnit 11.0.x by Sebastian Bergmann and contributors.
....... 7 / 7 (100%)
Time: 00:00.012, Memory: 6.00 MB
OK (7 tests, 15 assertions)
TestDox 格式:
Calculator
✔ Add
✔ Subtract
✔ Multiply
✔ Divide
✔ Divide by zero throws exception
✔ Fibonacci with data set "zero"
✔ Fibonacci with data set "one"
✔ Fibonacci with data set "five"
✔ Fibonacci with data set "ten"
✔ Fibonacci negative throws exception
常用断言速查
| 断言 | 用途 |
|---|---|
assertSame($a, $b) |
严格等于(类型+值) |
assertEquals($a, $b) |
宽松等于 |
assertTrue($x) |
为 true |
assertFalse($x) |
为 false |
assertNull($x) |
为 null |
assertCount($n, $arr) |
数组元素数 |
assertContains($v, $arr) |
包含某值 |
expectException(Class::class) |
预期异常 |