テストコードにパスワードを直書きしたくない
AdminLoginCept.php
$I = new AcceptanceTester($scenario);
$I->wantToTest('管理画面にログイン');
$I->amOnPage('/admin/');
$I->seeCurrentURLEquals('/admin/login');
$I->fillField('login_id', 'hideki');
$I->fillField('password', 'hidekisuteki'); // 👎
$I->click('ログイン');
$I->seeCurrentURLEquals('/admin/');
- 直書きせずに、環境変数ファイルから取得する方法があるはずと思って調べたのですが、思ったより面倒だったので書き残しておきます。
Codeception Config Module をインストール
Loads params from Codeception config and pass them to test scenarios.
composer require-dev justblackbird/codeception-config-module
composer.json
"require-dev": {
"codeception/codeception": "^4.1",
"justblackbird/codeception-config-module": "^1.0",
}
codeception.yml
にパラメーター設定を追加
.env
ADMIN_PASSWORD=hidekisuteki
codeception.yml
paths:
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
actor_suffix: Tester
extensions:
enabled:
- Codeception\Extension\RunFailed
params:
- .env # 👈
- これで、
.env
ファイル内の環境変数を設定ファイルの中で使えるようになります(テストコード内ではまだです)。
acceptance.suite.yml
にモジュール設定を追加
acceptance.suite.yml
actor: AcceptanceTester
modules:
enabled:
- PhpBrowser:
url: http://localhost/
- \Helper\Acceptance
- Config:
password: "%ADMIN_PASSWORD%" # 👈
-
.env
内の使いたいパラメーターの名前を、%
で囲って記述します。
テストコード内でモジュールを使用
AdminLoginCept.php
$I = new AcceptanceTester($scenario);
$I->wantToTest('管理画面にログイン');
$I->amOnPage('/admin/');
$I->seeCurrentURLEquals('/admin/login');
$I->fillField('login_id', 'hideki');
$I->fillField('password', $I->grabFromConfig('password')); // 👍
$I->click('ログイン');
$I->seeCurrentURLEquals('/admin/');