Codeception 异常行为(不是 运行 所有使用 Yii2 的 CEPT 任务)
Codeception undexpected behaviuor (not running all CEPT tasks with Yii2)
我有一个带有 4 个测试函数的测试 class。当我 运行 codecept 运行 时,只有 1 个函数被调用并且没有应用任何断言。
我用的是Yii2高级模板,frontend文件夹是REST用的API(过段时间我会重命名的)。我尝试编写 API 测试,就像在 the guide 中定义的那样,如果我将每个测试都放在不同的文件中,它就可以工作,就像在指南中一样。但是在每次测试之前都不会清除数据库,并且在不同的文件中进行类似的测试太不舒服了,更不用说固定装置了。
配置:
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',
],
]
);
}
}
正在尝试 运行 测试:
我很惊讶你的测试能运行。
Cept 格式适用于没有函数或 classes: 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
这看起来像你的例子,但你必须在文件名和 class 名称中使用 Cest 后缀而不是 Cept。
我有一个带有 4 个测试函数的测试 class。当我 运行 codecept 运行 时,只有 1 个函数被调用并且没有应用任何断言。
我用的是Yii2高级模板,frontend文件夹是REST用的API(过段时间我会重命名的)。我尝试编写 API 测试,就像在 the guide 中定义的那样,如果我将每个测试都放在不同的文件中,它就可以工作,就像在指南中一样。但是在每次测试之前都不会清除数据库,并且在不同的文件中进行类似的测试太不舒服了,更不用说固定装置了。
配置:
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',
],
]
);
}
}
正在尝试 运行 测试:
我很惊讶你的测试能运行。 Cept 格式适用于没有函数或 classes: 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
这看起来像你的例子,但你必须在文件名和 class 名称中使用 Cest 后缀而不是 Cept。