扩展抽象测试类时找不到测试类

Posted

技术标签:

【中文标题】扩展抽象测试类时找不到测试类【英文标题】:Test class not found when extending abstract test class 【发布时间】:2019-03-30 22:40:24 【问题描述】:

我目前正在使用 lumen 框架 (v5.6) 并为我的代码编写单元测试。

我有一个基类TestCase:

namespace Tests;

$_SERVER["http_proxy"] = "";

abstract class TestCase extends \Laravel\Lumen\Testing\TestCase

    /**
     * Creates the application.
     *
     * @return \Laravel\Lumen\Application
     */
    public function createApplication()
    
        return require __DIR__.'/../bootstrap/app.php';
    

我使用这个基类来编写我的测试,但是我有两个测试有很多重叠(它们都测试接口的实现),所以我把通用逻辑放在一个抽象类中:

namespace Tests\App\IO;

use App\io\PageDataParser;
use App\Models\AdvancedArray;
use App\Services\PageService;
use Mockery;
use Tests\TestCase;

abstract class ParsePageDataTestCase extends TestCase


    // Test logic here, but not relevant for the question


最后我在实际测试中使用了这个抽象类:

namespace Tests\App\IO;

use App\io\JsonPageDataParser;
use App\Models\AdvancedArray;

class JsonParsePageDataTestCaseTest extends ParsePageDataTestCase


   // Test are here, but not relevant for the question

但是,当我执行 JsonParsePageDataTestCaseTest 时,我收到以下错误:

PHP 致命错误:在第 15 行的 \tests\app\io\JsonParsePageDataTest.php 中找不到类“Tests\ParsePageDataTestCase”

我已验证文件夹的结构已更正,也尝试使用'composer dump-autoloadand verified that mycomposer.jsonhas an entry which specifies a classmap to 'tests/

我使用加载bootstrap/app.php 的phpunit.xml 执行我的测试,但我仍然收到此错误。

phpunit.xml:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/app.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Application test Suite">
            <directory suffix="Test.php">./tests</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
           <!-- ENV variables go here -->

    </php>
</phpunit> 

最后是我的composer.json


    "name": "laravel/lumen",
    "description": "The Laravel Lumen Framework.",
    "keywords": ["framework", "laravel", "lumen"],
    "license": "MIT",
    "type": "project",
    "require": 
        "php": ">=7.1.3",
        "laravel/lumen-framework": "5.6.*",
        "vlucas/phpdotenv": "~2.2",
        "willdurand/hateoas": "~2.1",
        "guzzlehttp/guzzle": "~6.0",
        "ext-json": "*",
        "vinelab/neoeloquent": "^1.4.6",
        "jenssegers/mongodb": "3.4.*",
        "predis/predis": "~1.0"
    ,
    "require-dev": 
        "fzaninotto/faker": "~1.4",
        "phpunit/phpunit": "~7.0",
        "mockery/mockery": "~1.0",
        "barryvdh/laravel-ide-helper": "~2.5"
    ,
    "autoload": 
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "psr-4": 
            "App\\": "app/"
        
    ,
    "autoload-dev": 
        "classmap": [
            "tests/"
        ]
    ,
    "scripts": 
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ]
    ,
    "config": 
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true
    ,
    "minimum-stability": "dev",
    "prefer-stable": true

如果您需要更多信息,请告诉我。

提前谢谢你!

【问题讨论】:

在 JsonParsePageDataTestCaseTest.php 上的命名空间声明后缺少 ParsePageDataTestCase 使用语句。 @GabrielPereira,感谢您的回复。但它们位于同一个命名空间中,因此不需要使用语句 你能分享你的 composer.json 自动加载吗? @GabrielPereira 是的,我更新了问题 您是否尝试过加载您的测试类,将它们放在 psr-4 上自动加载为 "Test\\": "tests/" ? 【参考方案1】:

这是 Lumen 安装的问题。 当您安装 laravel 时,测试文件夹在 autoload-dev 上配置为 psr-4:


"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
...
"autoload-dev": 
    "psr-4": 
        "Tests\\": "tests/"
    
,
...
"minimum-stability": "dev",
"prefer-stable": true

但在 Lumen 上安装并不像我们在下面看到的那样:


"name": "laravel/lumen",
"description": "The Laravel Lumen Framework.",
"keywords": ["framework", "laravel", "lumen"],
"license": "MIT",
"type": "project",
"require": 
    "php": ">=7.1.3",
    "laravel/lumen-framework": "5.7.*",
    "vlucas/phpdotenv": "~2.2"
,
"require-dev": 
    "fzaninotto/faker": "~1.4",
    "phpunit/phpunit": "~7.0",
    "mockery/mockery": "~1.0"
,
"autoload": 
    "classmap": [
        "database/seeds",
        "database/factories"
    ],
    "psr-4": 
        "App\\": "app/"
    
,
"autoload-dev": 
    "classmap": [
        "tests/"
    ]
,
"scripts": 
    "post-root-package-install": [
        "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
    ]
,
"config": 
    "preferred-install": "dist",
    "sort-packages": true,
    "optimize-autoloader": true
,
"minimum-stability": "dev",
"prefer-stable": true

因此,为了使其正常工作,您需要将自动加载更改为:

"autoload-dev": 
    "classmap": [
        "tests/"
    ],
    "psr-4": 
        "Tests\\": "tests/"
    

【讨论】:

以上是关于扩展抽象测试类时找不到测试类的主要内容,如果未能解决你的问题,请参考以下文章

运行 JUnit 测试时找不到类异常

测试新模型时找不到 Eloquent 类

在单元测试中模拟时找不到 Laravel 外观类

测试扩展类时的 ES6 基类(超级方法)的玩笑模拟方法

javac编译时找不到文件的问题和运行项目找不到指定类问题

安装数据表类时找不到