安装PHPUnit
虽然官网给了介绍,但是我还是根据自己的配置来讲一下。
环境配置
- 首先使用
brew
安装phpunit
brew install phpunit
- 使用
composer
安装phpunit
composer require phpunit/phpunit
测试
先随便写一个类:
class UserStore{
public function sayHello(){
print "hello world\r\n";
}
}
再来写对应的测试类:
// 调用composer 中的 phpunit
require_once __DIR__."/../../vendor/autoload.php";
// 路径根据你自己的需要来设置
require_once __DIR__."/../UserStore.php";
use PHPUnit\Framework\TestCase;
/**
* Class UserStoreTest
*/
class UserStoreTest extends TestCase{
/**
* @test
*/
public function testSayHello(){
$userStore=new UserStore();
$userStore->sayHello();
}
}
$userStoreTest=new UserStoreTest();
最后来运行这个类,这里我的文件名是UserStoreTest.php
。
phpunit UserStoreTest.php
输出结果为这样:
出现这样的内容,你的环境基本上就配置好了。