Codeception 测试用例前提条件
Codeception test cases precondition
我正在使用 codeception 框架编写自动化测试。我有一个测试用例,它在用户登录后验证某些功能。大约有 20 个具有不同功能的测试用例。所有的测试用例都需要一个用户登录系统,所以我在_before回调下写了一个登录功能。当我执行所有测试用例时,在每个测试用例之前检查登录功能,这会花费很多时间。我们能否将登录功能编写为前提条件,一旦用户登录就应该执行所有测试用例?
您可以在代码接收中使用所谓的助手。您可以使用以下命令生成帮助程序:
vendor/bin/codecept generate:helper Login
然后你可以在 class 中添加一个方法来像这样登录用户:
<?php
namespace Helper;
// here you can define custom actions
// all public methods declared in helper class will be available in $I
class Login extends \Codeception\Module
{
public function login($username, $password)
{
/** @var \Codeception\Module\WebDriver $webDriver */
$webDriver = $this->getModule('WebDriver');
// if snapshot exists - skipping login
if ($webDriver->loadSessionSnapshot('login')) {
return;
}
// logging in
$webDriver->amOnPage('/login');
$webDriver->submitForm('#loginForm', [
'login' => $username,
'password' => $password
]);
$webDriver->see($username, '.navbar');
// saving snapshot
$webDriver->saveSessionSnapshot('login');
}
}
有关快照的详细信息,请参阅 http://codeception.com/docs/06-ReusingTestCode#session-snapshot。
您的 acceptance.suite.yml 应如下所示:
# Codeception Test Suite Configuration
#
# Suite for acceptance tests.
# Perform tests in browser using the WebDriver or PhpBrowser.
# If you need both WebDriver and PHPBrowser tests - create a separate suite.
class_name: AcceptanceTester
modules:
enabled:
# Note we must use WebDriver for us to use session snapshots
- WebDriver:
url: http://localhost/myapp
browser: chrome
- \Helper\Acceptance
# Note that we must add the Login Helper class we generated here
- \Helper\Login
现在我们有一个助手 class 可以在我们所有的测试中重复使用。让我们看一个例子:
<?php
class UserCest
{
// tests
public function testUserCanLogin(AcceptanceTester $I)
{
$I->login('username', 'password');
}
public function testUserCanCarryOutTask(AcceptanceTester $I)
{
$I->login('username', 'password');
$I->amOnPage('/task-page');
$I->see('Task Page');
// other assertions below
}
public function testUserCanCarryOutAnotherTask(AcceptanceTester $I)
{
$I->login('username', 'password');
$I->amOnPage('/another-task-page');
$I->see('Another Task Page');
// other assertions below
}
}
现在 运行 UserCest 测试时,它应该只让用户登录一次。
我正在使用 codeception 框架编写自动化测试。我有一个测试用例,它在用户登录后验证某些功能。大约有 20 个具有不同功能的测试用例。所有的测试用例都需要一个用户登录系统,所以我在_before回调下写了一个登录功能。当我执行所有测试用例时,在每个测试用例之前检查登录功能,这会花费很多时间。我们能否将登录功能编写为前提条件,一旦用户登录就应该执行所有测试用例?
您可以在代码接收中使用所谓的助手。您可以使用以下命令生成帮助程序:
vendor/bin/codecept generate:helper Login
然后你可以在 class 中添加一个方法来像这样登录用户:
<?php
namespace Helper;
// here you can define custom actions
// all public methods declared in helper class will be available in $I
class Login extends \Codeception\Module
{
public function login($username, $password)
{
/** @var \Codeception\Module\WebDriver $webDriver */
$webDriver = $this->getModule('WebDriver');
// if snapshot exists - skipping login
if ($webDriver->loadSessionSnapshot('login')) {
return;
}
// logging in
$webDriver->amOnPage('/login');
$webDriver->submitForm('#loginForm', [
'login' => $username,
'password' => $password
]);
$webDriver->see($username, '.navbar');
// saving snapshot
$webDriver->saveSessionSnapshot('login');
}
}
有关快照的详细信息,请参阅 http://codeception.com/docs/06-ReusingTestCode#session-snapshot。
您的 acceptance.suite.yml 应如下所示:
# Codeception Test Suite Configuration
#
# Suite for acceptance tests.
# Perform tests in browser using the WebDriver or PhpBrowser.
# If you need both WebDriver and PHPBrowser tests - create a separate suite.
class_name: AcceptanceTester
modules:
enabled:
# Note we must use WebDriver for us to use session snapshots
- WebDriver:
url: http://localhost/myapp
browser: chrome
- \Helper\Acceptance
# Note that we must add the Login Helper class we generated here
- \Helper\Login
现在我们有一个助手 class 可以在我们所有的测试中重复使用。让我们看一个例子:
<?php
class UserCest
{
// tests
public function testUserCanLogin(AcceptanceTester $I)
{
$I->login('username', 'password');
}
public function testUserCanCarryOutTask(AcceptanceTester $I)
{
$I->login('username', 'password');
$I->amOnPage('/task-page');
$I->see('Task Page');
// other assertions below
}
public function testUserCanCarryOutAnotherTask(AcceptanceTester $I)
{
$I->login('username', 'password');
$I->amOnPage('/another-task-page');
$I->see('Another Task Page');
// other assertions below
}
}
现在 运行 UserCest 测试时,它应该只让用户登录一次。