composer安装phpunit
基础环境介绍:系统 mac php(7.1)
1.利用composer全局安装phpunit 命令:composer global require phpunit/phpunit
2.将phpunit添加进环境变量
1)sudo vim ~/.bash_profile
2)添加 PATH=${PATH}:/Users/zhengjiayu/.composer/bin
3)phpunit --version(测试是否安装成功)
3.在phpstorm中设置phpunit可用(下面直接上截图)



在项目中使用phpunit
1.用composer将phpunit包再次下载到当前目录,以防止出现类不存在错误,命令:composer require --dev phpunit/phpunit
2.开始写个例子(直接上代码截图)




3.运行测试文档
1)直接在phpstorm里点击运行(这种测试方法我一直报错,可能是自动加载有问题,暂时还未解决)
2)执行命令:phpunit -c /usr/local/var/www/modern/phpunit.xml [-c的作用是指定phpunit配置文件路径]
4.执行结果

5.生成代码覆盖率文件
1).命令:phpunit -c /usr/local/var/www/modern/phpunit.xml --coverage-html /usr/local/var/www/modern/tests/coverage (--coverage-html后面路径执行生成的文件目录地址)
2)覆盖率文件(截图)

注意:
1.使用composer自动加载时,composer.json里要定义目录和协议,不然自动加载无效
如:"autoload": {
"psr-4": {
"modern\\" : "/usr/local/var/www/modern"
}
}
2.使用phpunit编写test文档时,类名称后缀一定是Test,方法名一定时test开头
网友评论