安装
composer install phpunit/phpunit
创建测试
推荐目录结构
下面目录结构使用
tree /f
命令得到
└─tests
└─unit
ClassNameTest.php
└─integration
ClassNameTest.php
单元测试类实例
namespace path_to_namespace;
use PHPUnit\Framework\TestCase;
use path_to_class\ClassName;
class ClassNameTest extends TestCase
{
public function testName()
{
$this->assertEquals(5,(new ClassName())->some_function());
}
}
运行测试用例
使用命令行
进入
phpunit
目录,如果是composer
安装,一般路径是vendor/composer/bin/
cd vendor/bin
phpunit path_to_class_unit_test/ClassNameTest.php
测试结果示例
PHPUnit 7.5.14 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 130 ms, Memory: 4.00 MB
OK (1 test, 1 assertion)
使用 IDE
集成环境
这里以
phpstorm
为例
在单元测试用例类中,右键->选择 Run 'ClassNameTest(PHPUnit)'
即可
测试结果示例
Testing started at 15:25 ...
C:\phpStudy\PHPTutorial\php\php-7.2.1-nts\php.exe D:/qinyi/file_tools/vendor/phpunit/phpunit/phpunit --no-configuration --filter "/(::testTimeOut5)( .*)?$/" Singi\src\tests\unit\CurlHelperTest D:\qinyi\file_tools\src\tests\unit\CurlHelperTest.php --teamcity
PHPUnit 7.5.14 by Sebastian Bergmann and contributors.
Time: 119 ms, Memory: 4.00 MB
OK (1 test, 1 assertion)
Process finished with exit code 0