https://phpunit.de/
官方文档下载:
https://phpunit.de/manual/current/zh_cn/phpunit-book.pdf
要进行单元测试的情况,可能有以下三种:
- 在开发完成时或开发过程中,对某个函数、方法边调试边进行测试。测试案例可能在进行开发的同时撰写,或者在项目的详细设计阶段即已经写好;
- 对一个模块(包含多个功能点)中的所有功能点进行一些集中的测试以检查是不是每一个功能点都能通过测试;
- 对于整个项目的统一单元测试。通常与每日构造结合。
安装方法
wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
mv phpunit.phar /usr/local/bin/phpunit
phpunit --version
或者是直接使用
php phpunit.phar --version
测试用例
<?php
class Money {
private $amount;
public function __construct($amount) {
$this->amount = $amount;
}
public function getAmount() {
return $this->amount;
}
public function saveAmount($count) {
$this->amount += $count;
return $this->amount;
}
public function loseAmount($count) {
$this->amount = $this->amount - $count;
return $this->amount;
}
public function good() {
$this->amount + 1000;
return $this->amount;
}
}
<?php
include_once('./Money.php');
use phpunit\framework\TestCase;
class MoneyTest extends TestCase {
public function testGetAmount() {
$a = new Money(22);
echo $a->getAmount();
return ;
}
public function testSaveAmount() {
$a = new Money(43);
echo $a->saveAmount(20);
return ;
}
public function testLoseAmount() {
$a = new Money(33);
echo $a->loseAmount(1);
return ;
}
public function testGood() {
$a = new Money(1000);
echo $a->good();
return ;
}
}
phpunit MoneyTest.php
结果如下
PHPUnit 5.4.6 by Sebastian Bergmann and contributors.
.22.63.32. 4 / 4 (100%)1000
Time: 173 ms, Memory: 8.00MB
OK (4 tests, 0 assertions)