繁体   English   中英

Yii2和Codeception API测试_before

[英]Yii2 and Codeception API test with _before

我使用Yii2作为我的应用程序框架,我从高级模板开始。 我的应用程序有一个前端,后端和api入口点。 我已经将前端,后端和api从根目录移到了/ modules中,以使根目录杂乱无章。

我正在尝试为我的API注册端点创建注册测试。 在运行测试之前,我需要确保进行测试注册的电子邮件地址尚未在测试数据库中注册。

我正在使用Cest _before方法来查看是否可以通过测试电子邮件找到会员模型,如果找到一个,则将其删除。

问题在于这导致测试在没有任何调试信息的情况下退出。

这是我应该如何进行测试设置的方法吗? 我应该做其他事情来清除所有旧测试用户吗?

如果删除_before()设置功能,则该测试将在我第一次运行时运行。

AuthenticationCest.php

 namespace tests\codeception\api\api;

use common\models\Member;
use tests\codeception\api\ApiTester;

class AuthenticationCest {

    public $email = "reg.test@member.com";

    //Check to see id the email is already used and delete it
    public function _before(ApiTester $I) {
        $member = Member::findOne([
            "email" => $this->email
        ]);

        if ($member) {
            $member->delete();
        }
    }

    //Test that a member can register
    public function registerTest(ApiTester $I) {
        $I->wantTo('Register a new member via the API');

        $I->haveHttpHeader('Content-Type', 'application/json');

        $I->sendPOST('token', [
            'grant_type'    => 'registration',
            'client_id'     => 'testclient',
            'client_secret' => 'testpass',
            'email'         => $this->email,
            'password'      => 'Test1234',
            'given_name'    => 'Test',
            'family_name'   => 'Member'
        ]);

        $I->seeResponseCodeIs(200);
        $I->seeResponseIsJson();
    }
}

_bootstrap.php

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'test');

defined('YII_APP_BASE_PATH') or define('YII_APP_BASE_PATH', dirname(dirname(dirname(__DIR__))));

require_once(YII_APP_BASE_PATH . '/vendor/autoload.php');
require_once(YII_APP_BASE_PATH . '/vendor/yiisoft/yii2/Yii.php');
require_once(YII_APP_BASE_PATH . '/common/config/bootstrap.php');
require_once(YII_APP_BASE_PATH . '/modules/api/config/bootstrap.php');

Yii::setAlias('@tests', dirname(dirname(__DIR__)));

这似乎足以让我创建和删除用于单元测试的模型,但不能创建功能性API测试

api.suite.yml

class_name: ApiTester
modules:
enabled: [PhpBrowser, REST]
config:
  PhpBrowser:
      url: http://api.yii.lan/
  REST:
      url: http://api.yii.lan/v1/

编辑输出

$ ../vendor/bin/codecept run --steps --debug -vvv

Codeception PHP Testing Framework v2.0.13
Powered by PHPUnit 4.6.6 by Sebastian Bergmann and contributors.

[tests\codeception\common]: tests from /workspace/yii2/tests//codeception/common


Tests\codeception\api.api Tests (3) ------------------------------------------------------------------------
Modules: PhpBrowser, REST, Db
------------------------------------------------------------------------------------------------------------------------------------
Register via the API (tests\codeception\api\api\oauth\RegistrationCest::registerTest)
Scenario:

如果每次测试后都需要清除数据库,则需要使用Fixtures。 夹具是测试的重要组成部分。 它们的主要目的是将环境设置为固定/已知状态,以便您的测试可重复并且以预期的方式运行。 有关它们的更多信息, 请访问http://www.yiiframework.com/doc-2.0/guide-test-fixtures.html

阅读文档后,您发现您已经有了FixtureHelper,在高级模板中,它应该在/ tests / codeception / common / support / FixtureHelper中。

您应该在tests /.../ common / fixtures文件夹中生成一个名为MemberFixture.php的文件。 之后,您应该在数据库中生成数据(几个成员将成为测试对象)。

最后,您应该将FixtureHelper添加到您的.yml文件(functional.yml)。 例如,我的accept.yml看起来像

class_name: AcceptanceTester
modules:
enabled:
  - PhpBrowser
  - REST
  - ApiHelper
  - tests\codeception\common\_support\FixtureHelper
  - Db
config:
  PhpBrowser:
      url: http://localhost:8080
  REST:
      url: http://localhost:8080/frontend/web/index-test.php
  Db:
    dsn: 'mysql:host=localhost;dbname=y1db_test'
    user: 'newproject'
    password: 'newproject'
    populate: false,
    cleanup: false

就是这样,现在在每次测试之后,您的成员表将被清理,并使用灯具中的标准具数据进行填充。 测试将始终为绿色。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM