【问题标题】:Codeception can't find class when running single test运行单个测试时,Codeception 找不到类
【发布时间】:2017-09-16 20:52:21
【问题描述】:

我正在为我目前正在进行的项目编写 API 测试套件。

我可以使用成功运行套件

codecept run api

并且所有测试都成功通过了,但是每当我尝试使用

运行单个测试时
codecept run api Tests\Users\Get

抛出以下错误:

PHP 致命错误:在第 12 行的 /Users/Username/Programming/PHP/Project/tests/api/Tests/Users/GetCest.php 中找不到类“Tests\ApiCest”

这是我正在使用的文件夹结构:

ApiCest.php 仅用于快速引导其他测试。这是它的内容:

namespace Tests;

/*
 * This class is extended by the API tests.
 */
use ApiTester;

class ApiCest
{
    public function _before(ApiTester $I)
    {
        $I->prepareRequest();
    }

    public function _after(ApiTester $I)
    {
        // All API responses must be in a JSON format.
        $I->seeResponseIsJson();
    }
}

这就是它在GetCest.php中的使用方式:

namespace Tests\Users;

use ApiTester;
use Codeception\Util\HttpCode;
use Tests\ApiCest;

/*
 * Tests for the GET /users API endpoint.
 */
class GetCest extends ApiCest
{
    // Tests methods are here but omitted
}

【问题讨论】:

    标签: php composer-php codeception


    【解决方案1】:

    这是因为 Tests\Users\GetCest 类扩展了 Tests\ApiCest 类,但没有为测试类设置自动加载。

    将此块添加到您的 composer.json 文件中:

    "autoload":{
        "psr-4":{
            "Tests\\": "tests/api/Tests"
        }
    }
    

    如果您已经有 autoload psr-4 块,只需添加 "Tests\\": "tests/api/Tests" 行。

    【讨论】:

    • 谢谢。我以前试过这样做,但没有奏效。我想我做的不对:)哦,我也用autoload-dev而不是autoload
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-27
    • 2017-04-03
    • 1970-01-01
    • 2012-07-08
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多