在 composer.json 的 scripts 中配置 PHPUnit 命令(如 "test": "phpunit"),可实现 composer test 一键运行测试;需先以 --dev 安装 phpunit/phpunit,配置 phpunit.xml,并支持 verbose、coverage、CI 等多场景脚本。

在 Composer 的 composer.json 中配置 PHPUnit 测试脚本,能让测试像运行命令一样简单——比如直接执行 composer test 就自动跑完所有用例,无需记住完整路径或参数。
1. 确保 PHPUnit 已正确安装
推荐以开发依赖方式安装 PHPUnit(兼容现代版本):
- PHP 8.1+ 项目建议用 PHPUnit 10.x:
composer require --dev phpunit/phpunit:^10 - 安装后检查是否可执行:
./vendor/bin/phpunit --version - 确保项目根目录下有
phpunit.xml或phpunit.xml.dist配置文件(定义测试目录、引导文件、覆盖率等)
2. 在 composer.json 的 scripts 段添加测试命令
编辑 composer.json,在 "scripts" 下加入自定义脚本:
"scripts": {
"test": "phpunit",
"test-verbose": "phpunit --verbose",
"test-cover": "phpunit --coverage-html coverage"
}登录后复制
说明:
立即学习“PHP免费学习笔记(深入)”;
标签: php linux html js git json composer windows 工具 mac macos win
还木有评论哦,快来抢沙发吧~