一、先安装PHPUnit,在terminal执行
$ wget https://phar.phpunit.de/phpunit.phar $ chmod +x phpunit.phar $ sudo mv phpunit.phar /usr/local/bin/phpunit
然后:phpunit --version (注:当前版本的phpunit需要PHP7)
二、配置phpstorm
1、Preferences =》Languages & Frameworks =》PHP =》Include path中添加/usr/local/bin
2、Preferences =》Languages & Frameworks =》PHP =》PHPUnit =》Path to phpunit.phar
三、写测试用例
新建一个类:
<?php
/**
* Created by PhpStorm.
* User: wangpeng
* Date: 2018/6/1
* Time: 14:22
*/
class Money
{
function add( $a, $b ) { return $a+$b; }
}
再添加对应的测试用例
<?php
require_once 'Money.php';
use \PHPUnit\Framework\PHPUnit_Framework_TestCase;
/**
* Created by PhpStorm.
* User: wangpeng
* Date: 2018/6/1
* Time: 14:23
*/
class MoneyTest extends PHPUnit\Framework\TestCase
{
function test1() {
$money=new money();
$this->assertEquals(3, $money->add( 1, 2 ) );
}
}
然后跑起来,不出意外,会出现如下截图: