随便用 Google 百度一下就可以找到这个: PHPUnit – The PHP Testing Framework

貌似也还有别的类似框架或者自己写 PHP 脚本来完成,但这个就不在我们今天的讨论范围啦。

Getting Started with PHPUnit

Download

We distribute a PHP Archive (PHAR) that contains everything you need in order to use PHPUnit. Simply download it from here, make it executable, and put it into your $PATH, for instance:

1
2
3
4
wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
sudo mv phpunit.phar /usr/local/bin/phpunit
phpunit --version

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
class Money {
    private $amount;

    public function __construct($amount) {
        $this->amount = $amount;
    }

    public function getAmount() {
        return $this->amount;
    }

    public function negate() {
        return new Money(-1 * $this->amount);
    }

    // ...
}

TestCode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
use PHPUnit\Framework\TestCase;

class MoneyTest extends TestCase {
    public function testCanBeNegated() {
        // Arrange
        $a = new Money(1);

        // Act
        $b = $a->negate();

        // Assert
        $this->assertEquals(-1, $b->getAmount());
    }

    // ...
}

Run

1
2
3
4
5
6
7
8
➜  PHPUnitExample git:(master) ✗ phpunit --bootstrap src/Monkey.php tests/MonkeyTest.php
PHPUnit 5.7.5 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 95 ms, Memory: 8.00MB

OK (1 test, 1 assertion)

比较好理解,加载一个类,然后执行相应的单元测试。 PHPUnit 完整例子

还要看相应的文档,了解一下高级用法。

高级用法(TODO)

Refs

Getting Started with PHPUnit