簡體   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