You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 10用artisan test无异常抛出但phpunit正常的问题求助

Laravel 10中artisan test不抛出异常仅提示无断言的问题解决

问题描述

Laravel 8升级到Laravel 10后,业务功能正常,但测试环节出现异常:采用TDD模式编写测试用例,请求未定义的路由名称时,使用php artisan test --filter=test_get_home_page运行测试,预期抛出Symfony\Component\Routing\Exception\RouteNotFoundException异常,但实际仅返回This test did not perform any assertions提示;无论测试中存在何种错误(如调用不存在的方法),artisan test都会返回相同提示,而直接使用phpunit命令运行时,能正常抛出预期错误。

测试代码

<?php

namespace Tests\Feature;

use Tests\TestCase;

class SanityCheckTest extends TestCase
{
    public function test_get_home_page()
    {
        $this->get(route('home'))->assertOk();
    } 
}

artisan test运行输出

WARN  Tests\Feature\sanityCheckTest
! get home page → This test did not perform any assertions                                     0.77s

Tests:    1 risky (0 assertions)
Duration: 1.11s

phpunit运行输出

PHPUnit 10.1.2 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.5
Configuration: /var/www/html/phpunit.xml

E                                                                   1 / 1 (100%)

Time: 00:00.917, Memory: 18.00 MB

There was 1 error:

1) Tests\Feature\sanityCheckTest::test_get_home_page
   Symfony\Component\Routing\Exception\RouteNotFoundException: Route [home] not defined.

/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php:467
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:810
/var/www/html/tests/Feature/sanityCheckTest.php:11
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:174

--

There was 1 risky test:

1) Tests\Feature\sanityCheckTest::test_get_home_page
   This test did not perform any assertions

/var/www/html/tests/Feature/sanityCheckTest.php:9

ERRORS!
Tests: 1, Assertions: 0, Errors: 1, Risky: 1.

已排查步骤

  • 在全新Laravel 10项目中运行相同测试,结果符合预期
  • 注释Tests/TestCase.php和Tests/CreateApplication.php中的所有自定义代码,问题依旧
  • 对比/artisan、/App/Console/Kernel.php、/App/Http/Kernel.php、/App/Exceptions/Handler.php与全新Laravel 10项目的对应文件,未发现异常

解决方案

1. 检查异常处理类的全局捕获逻辑

打开App\Exceptions\Handler.php,检查register方法中是否存在全局捕获异常并静默处理的逻辑。测试环境下应确保异常被重新抛出,而非转换为响应或忽略:

public function register()
{
    $this->reportable(function (Throwable $e) {
        // 仅在非测试环境执行异常上报逻辑
        if (!app()->environment('testing')) {
            // 你的上报代码
        }
    });

    $this->renderable(function (Throwable $e, $request) {
        // 测试环境下直接抛出异常,不渲染HTTP响应
        if (app()->environment('testing')) {
            throw $e;
        }
        // 其他环境的响应渲染逻辑
        // ...
    });
}

2. 核对测试环境配置

检查phpunit.xml文件,确保测试环境的配置与全新Laravel 10项目一致,重点确认:

  • APP_ENV设置为testing
  • APP_DEBUG设置为true
  • 数据库、缓存等驱动配置正确

示例标准配置片段:

<php>
    <env name="APP_ENV" value="testing"/>
    <env name="APP_DEBUG" value="true"/>
    <env name="BCRYPT_ROUNDS" value="4"/>
    <env name="CACHE_DRIVER" value="array"/>
    <env name="DB_CONNECTION" value="sqlite"/>
    <env name="DB_DATABASE" value=":memory:"/>
    <env name="MAIL_MAILER" value="array"/>
    <env name="QUEUE_CONNECTION" value="sync"/>
    <env name="SESSION_DRIVER" value="array"/>
</php>

3. 清除缓存与重新加载自动加载

运行以下命令清除项目缓存并重新生成自动加载文件:

php artisan config:clear
php artisan route:clear
php artisan cache:clear
composer dump-autoload

4. 更新Laravel到最新版本

如果上述方法无效,尝试将Laravel框架更新到最新稳定版,可能是升级过程中存在的已知bug已被修复:

composer update laravel/framework

内容的提问来源于stack exchange,提问作者Andy BnOne

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 08:44:53