PHP项目Laravel测试覆盖率如何查看

wen PHP项目 3

本文目录导读:

PHP项目Laravel测试覆盖率如何查看

  1. 安装代码覆盖率扩展
  2. 生成测试覆盖率报告
  3. 配置 PHPUnit
  4. 查看覆盖率报告
  5. 常见忽略文件的配置
  6. CI/CD 集成配置
  7. 注意事项
  8. 扩展工具

在 Laravel 项目中查看测试覆盖率,主要通过 XdebugPCOV 扩展配合 PHPUnit 来实现,以下是完整的配置和查看方法:

安装代码覆盖率扩展

安装 Xdebug(推荐)

# Ubuntu/Debian
sudo apt-get install php-xdebug
# macOS (Homebrew)
brew install php-xdebug
# 或者通过 pecl
pecl install xdebug

安装 PCOV(更轻量)

pecl install pcov

在 php.ini 中启用

; Xdebug 3.x
xdebug.mode=coverage
; 或 PCOV
extension=pcov.so

生成测试覆盖率报告

基本命令

# 生成覆盖率报告
php artisan test --coverage
# 生成 HTML 格式报告(推荐)
php artisan test --coverage --coverage-html=coverage-report
# 生成 XML 格式(CI/CD 使用)
php artisan test --coverage --coverage-clover=coverage.xml
# 限制最小覆盖率(不达标则失败)
php artisan test --coverage --min-coverage=80

指定测试文件

# 测试指定文件
php artisan test --coverage tests/Feature/UserTest.php
# 测试指定目录
php artisan test --coverage tests/Feature/

配置 PHPUnit

phpunit.xml 中配置覆盖率:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true">
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <!-- 覆盖率配置 -->
    <source>
        <include>
            <directory suffix=".php">./app</directory>
        </include>
        <exclude>
            <directory suffix=".php">./app/Providers</directory>
            <directory suffix=".php">./app/Console</directory>
            <file>./app/Http/Kernel.php</file>
        </exclude>
    </source>
    <coverage>
        <report>
            <html outputDirectory="coverage-report"/>
            <clover outputFile="coverage.xml"/>
        </report>
    </coverage>
</phpunit>

查看覆盖率报告

命令行查看

运行测试后,终端会显示类似结果:

---------------- ---------- ---------- ---------- ---------- ----------
Class             Coverage    Method      Lines       
---------------- ---------- ---------- ---------- ---------- ----------
App\Http\          
  Controllers     88.4%      89.2%       87.6%      
App\Models        
  User            92.1%      90.0%       95.2%      
---------------- ---------- ---------- ---------- ---------- ----------
Total             85.6%      87.3%       84.8%      
---------------- ---------- ---------- ---------- ---------- ----------

HTML 报告

php artisan test --coverage-html=coverage-report

然后打开 coverage-report/index.html 文件,在浏览器中查看详细报告。

常见忽略文件的配置

<source>
    <include>
        <directory suffix=".php">./app</directory>
    </include>
    <exclude>
        <!-- 排除迁移文件 -->
        <directory suffix=".php">./database</directory>
        <!-- 排除配置文件 -->
        <directory suffix=".php">./config</directory>
        <!-- 排除路由文件 -->
        <file>./routes/web.php</file>
        <!-- 排除特定目录 -->
        <directory>./app/Exceptions</directory>
    </exclude>
</source>

CI/CD 集成配置

在 GitHub Actions 或 GitLab CI 中:

# .github/workflows/test.yml
- name: Run tests with coverage
  run: |
    vendor/bin/phpunit --coverage-clover coverage.xml
    vendor/bin/phpunit --coverage-text
- name: Upload coverage to Codecov
  uses: codecov/codecov-action@v3
  with:
    file: ./coverage.xml

注意事项

  1. 性能影响:开启覆盖率会显著降低测试速度,建议只在需要时开启
  2. Xdebug 版本:Xdebug 3.x 需要在 php.ini 中设置 xdebug.mode=coverage
  3. 内存限制:大项目可能需要增加内存限制:
    php -d memory_limit=512M artisan test --coverage
  4. Docker 环境
    # 在 Docker 容器中运行
    docker-compose exec app php -d memory_limit=1G artisan test --coverage

扩展工具

PHPStan + 覆盖率

# 安装 PHPStan
composer require --dev phpstan/phpstan
# 运行
vendor/bin/phpstan analyse app tests

可视化工具(Laravel Coverage)

composer require --dev spacedemon/phpunit-coverage

这样配置后,你就能清晰地看到项目的测试覆盖情况,并定位到未被测试的代码区域,建议将覆盖率保持在 80% 以上,确保代码质量。

抱歉,评论功能暂时关闭!