1.简介
PHPUnit 是 PHP 程式语言中最常见的单元测试 (unit testing) 框架,PHPUnit 是参考 xUnit 架构利用 PHP
为什么要使用 PHPUnit 来测试呢?虽然,要做单元测试可以自己写程式来测试, 但是 PHPUnit 提供了一些测试时常用的 library 及解决测试时会遇到问题的方法,所以我们会使用 PHPUnit 来做单元测试。
3.安装过程
composer 全局安装
composer global require phpunit/phpunit
创建项目目录
mkdir test
初始化composer 仓库
composer init
在你的项目目录下引入
composer require --dev phpunit/phpunit
在项目目录中新建tests文件夹
单元测试文件
mkdir tests
新建phpunit.xml
为了引入autoload.php
<phpunit bootstrap="vendor/autoload.php"><testsuites><testsuite name="service"><directory>tests</directory></testsuite></testsuites>
</phpunit>
在tests目录中新建EqualsTest.php
<?phpuse PHPUnit\Framework\TestCase;class EqualsTest extends TestCase {public function testFailure(){$this->assertEquals(1, 0);}public function testFailure2(){$this->assertEquals('bar', 'baz');}public function testFailure3(){$this->assertEquals("foo\nbar\nbaz\n", "foo\nbah\nbaz\n");}
}
目录结构
执行

image.png
4.配置phpstorm

image.png
5.相关资源
手册:http://www.phpunit.cn/manual/7.0/zh_cn/index.html
https://www.yiichina.com/tutorial/1042
http://www.php.cn/php-weizijiaocheng-380671.html