在behat测试中使用random var来生成唯一的用户名

时间:2015-04-20 14:19:25

标签: behat

现在我正在使用以下内容创建用户

Given users:
| name | status | roles |
| kyle | 1 | authenticated user |
| cartman | 1 | admin |

是否有可能在这些名称中添加随机字符串?

2 个答案:

答案 0 :(得分:1)

如果我没有误解,你可以这样做。

<强>小黄瓜

Scenario: Create random users
  Given I create "3" users

<强> FeatureContext

var $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
var $status = [0, 1];
var $roles = ['authenticated user', 'admin', 'superman'];

/**
 * @Given /^I create "([^"]*)" users$/
 */
public function createDummyUsers($count)
{
    for ($i = 0; $i < $count; $i++) {
        $name = substr(str_shuffle($this->str), 0, 8);
        $status = $this->status[array_rand($this->status, 1)];
        $role = $this->roles[array_rand($this->roles, 1)];

        echo "You've just created $name - $status -$role" . PHP_EOL;
    }
}

<强>打印

You've just created mqBWAQJK - 1 - superman
You've just created WYuAZSco - 0 - admin
You've just created HCNWvVth - 1 - admin

You've just created EmLkVRpO - 1 -superman
You've just created pxWcsuPl - 1 -authenticated user
You've just created mLYrlKdz - 0 -superman

答案 1 :(得分:-1)

我已经创建了一个可以尝试的解决方案。

https://github.com/JordiGiros/MinkFieldRandomizer

MinkFieldRandomizer是一个随机(有意义)信息生成器,用于填充Behat Mink Selenium测试中的浏览器表单字段。它提供了以更现实的方式运行测试的选项,更改用于在每次运行的测试中填写表单的信息。

您可以通过Composer轻松将其添加到项目中。

一个例子:

Then Fills in form fields with provided table
| "f_outbound_accommodation_name" | "{RandomName(10)}" |
| "f_outbound_accommodation_phone_number" | "{RandomPhone(9)}" |
| "f_outbound_accommodation_address_1" | "{RandomText(10)}" |

我希望你试试吧! 欢迎您添加新的功能或分叉或按照您的需要进行操作。 干杯