Codeception未预期的行为(不使用Yii2运行所有CEPT任务)

时间:2018-02-01 14:20:05

标签: php yii2 yii2-advanced-app codeception

我有一个包含4个测试功能的测试类。当我运行 codecept run 时,只调用了一个函数,并且没有应用断言。

我使用Yii2高级模板, frontend 文件夹用于REST API(我稍后将其重命名)。我尝试编写像the guide中定义的API测试,如果我在不同的文件中进行每个测试,就像在指南中一样。但是在每次测试之前数据库都没有被清除,并且在不同的文件中进行类似的测试太不舒服了,更不用说灯具了。

配置:

class_name: ApiTester
modules:
    enabled:
        - REST:
            depends: PhpBrowser
            url: 'http://localhost:8000'
            part: [json]
        - Yii2:
            part: [orm, fixtures]
            entryScript: index-test.php

代码:

use Codeception\Util\HttpCode;

class RegisterCept
{
    public function successRegister(ApiTester $I)
    {
        $I->wantTo("register successfully");
        $params = [
            'username' => 'ApiTester',
            'password' => '12345678',
            'email' => 'ApiTester@ApiTester.yii'
        ];
        $I->sendPOST('/users/register', $params);
        $I->seeResponseCodeIs(HttpCode::CREATED);
        $I->canSeeResponseIsJson();
        $I->seeResponseContains('');
    }

    public function getMethod(ApiTester $I)
    {
        $I->wantTo('be sure I can\'t register by GET method');
        $params = [
            'username' => 'ApiTester',
            'password' => '12345678',
            'email' => 'ApiTester@ApiTester.yii'
        ];
        $I->sendGET('/users/register', $params);
        $I->seeResponseCodeIs(HttpCode::NOT_FOUND);
    }

    public function emptyRequest(ApiTester $I)
    {
        $I->wantTo('be sure I can\'t register a new user without a username, password or e-mail');
        $I->sendPOST('/users/register');
        $I->seeResponseCodeIs(HttpCode::BAD_REQUEST);
        $I->seeResponseIsJson();
        $I->seeResponseContainsJson(
            [
                'errors' => [
                    'username' => ['Username cannot be blank.'],
                    'email' => ['Email cannot be blank.'],
                    'password' => ['Password cannot be blank.'],
                ],
            ]
        );
    }

    public function disallowRepeatedRegister(ApiTester $I)
    {
        $I->wantTo("Not to register second time");

        $params = [
            'username' => 'ApiTester',
            'password' => '12345678',
            'email' => 'ApiTester@ApiTester.yii'
        ];
        $I->sendPOST('/users/register', $params);
        $I->seeResponseCodeIs(HttpCode::CREATED);
        $I->canSeeResponseIsJson();
        $I->seeResponseContains('');

        $I->sendPOST('/users/register', $params);
        $I->seeResponseCodeIs(HttpCode::BAD_REQUEST);
        $I->canSeeResponseIsJson();
        $I->canSeeResponseContainsJson(
            [
                'errors' => [
                    'username' => 'This username has already been taken',
                    'email' => 'This email address has already been taken',
                ],
            ]
        );

    }
}

尝试运行测试:

Running tests

1 个答案:

答案 0 :(得分:1)

我对你的测试运行感到惊讶。 Cept格式适用于没有函数或类的普通文件:http://codeception.com/docs/02-GettingStarted#Writing-a-Sample-Scenario

$I = new AcceptanceTester($scenario);
$I->sendPOST('/users/register', $params);
$I->seeResponseCodeIs(HttpCode::CREATED);
$I->canSeeResponseIsJson();
$I->seeResponseContains('');

为了在一个文件中包含多个测试方法,您必须使用Cest格式:http://codeception.com/docs/02-GettingStarted#Cept-Cest-and-Test-Formats

这看起来像你的例子,但你必须在文件名和类名中使用Cest后缀而不是Cept。