I'm using Yii2 as my app framework, I started with the advanced template. I have a frontend, backend and api entry point for my app. I have moved frontend, backend and api out of the root directory and into /modules to keep clutter down in the root.
I am trying to create a registration test for my API registration endpoint. Before I run the test I need to ensure that the email address i'm testing registration with has not already been registered in the test database.
I'm using the Cest _before method to see if I can find a Member model with the test email, if I find one I then delete it.
The problem is that this is causing the test to exit without any debugging information.
Is this how I should be doing my test setup? Should I be doing something else to clear any old test users.
The test will work the first time I run it if I remove the _before() setup functionality.
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__)));
This seems to be enough to let me create and delete models for Unit test, but not the functional API tests
api.suite.yml
class_name: ApiTester
modules:
enabled: [PhpBrowser, REST]
config:
PhpBrowser:
url: http://api.yii.lan/
REST:
url: http://api.yii.lan/v1/
EDIT output
$ ../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:
If you need clear database after each test , then you need to use Fixtures. Fixtures are an important part of testing. Their main purpose is to set up the environment in a fixed/known state so that your tests are repeatable and run in an expected way. More about them in http://www.yiiframework.com/doc-2.0/guide-test-fixtures.html
After reading the document, you found out that you already have FixtureHelper, in advanced template it should be in /tests/codeception/common/support/FixtureHelper.
You should generate the file called MemberFixture.php in tests/.../common/fixtures folder . After that you should generate data in database( a couple of members that will be etalons an tests).
In finall part you should add FixtureHelper to youre .yml file(functional.yml). For example my acceptance.yml looks like
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
Thats all, now after every test, youre member table will be cleaned and populated with etalon data from the fixtures. And test will be alwayse green.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.