运行单个测试时,Codeception无法找到类

时间:2017-04-20 09:42:00

标签: php composer-php codeception

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

我可以使用

成功运行套件
codecept run api

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

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

抛出以下错误:

  

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

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

Folder structure

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
}

1 个答案:

答案 0 :(得分:1)

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

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

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

如果您已经autoload psr-4阻止,请只添加"Tests\\": "tests/api/Tests"行。

相关问题