【问题标题】:Loop through multiple accounts in codeception functional test在代码接收功能测试中循环多个帐户
【发布时间】:2022-10-24 00:30:48
【问题描述】:
我有多个帐户:“userWithCertainRole”、“userWithAnotherRole”和“userWithTwoRoles”。我想对具有特定角色的所有这些帐户的特定页面进行功能测试。所有账户的功能测试都是一样的,所以我不想重复代码或制作多个php文件。有没有办法在一个功能测试中循环访问这三个帐户?
/**
* @var string|null
*/
protected ?string $account = 'userWithCertainRole';
/**
* @param FunctionalTester $I
*/
public function page(FunctionalTester $I)
{
$this->login($I);
$I->amOnPage('/page');
$I->dontSee('You cannot access this page with this role');
$I->see('Page header');
}
【问题讨论】:
标签:
php
functional-testing
codeception
【解决方案1】:
使用 PHPUnit 中的数据提供者,或 Codeception 特定的示例注释/属性来为测试方法提供数据。
见https://codeception.com/docs/AdvancedUsage#Examples-Attribute
/**
* @dataProvider getRoles
*/
public function page(FunctionalTester $I, Example $example)
{
$this->login($example['username'], $example['password']);
}
protected function getRoles(): array
{
// keys make test output easier to understand
return [
'userWithRole1' => ['username' => 'username1', 'password' => 'password1'],
'userWithRole2' => ['username' => 'username2', 'password' => 'password2'],
'userWithRole3' => ['username' => 'username3', 'password' => 'password3'],
];
}