简体   繁体   English

黄瓜:生成测试数据的最佳方法? 并存储以备将来使用?

[英]Cucumber: best way to generate test data? and store it for future use?

I'm still pretty new to Cucumber automation testing and I haven't found anything about this in documentation, so I'm guessing either it's not possible or it's done in a very different way. 我对Cucumber自动化测试还很陌生,而且我在文档中还没有找到任何相关信息,所以我猜测这是不可能的,或者是以非常不同的方式完成的。

The thing is, in Robot automation framework I'm used to working with variables like this: ${userName} and once set these can be shared by different tests. 问题是,在机器人自动化框架中,我习惯于使用这样的变量:$ {userName},一旦设置,这些变量可以由不同的测试共享。 Is that possible in cucumber? 黄瓜有可能吗?

The specific use case is: I'm using Java version of Cucumber, and I want to create a new user account that I'll use throughout all scenarios in the whole feature file: 特定的用例是:我正在使用Java版的Cucumber,并且我想创建一个新的用户帐户,该帐户将在整个功能文件的所有场景中使用:

Scenario: Create user account
    Given user navigates to the home page
    And user enters username and password  *(this is where I want to provide random data and have it stored for future use)*
    And user clicks on the register button
    Then user should see the default landing page

I can always create a random user name and password in my test implementation using java and store them in class-scoped variables for future use in all steps that require them. 我始终可以使用Java在测试实现中创建随机的用户名和密码,并将它们存储在类作用域变量中,以备将来在需要它们的所有步骤中使用。 But I'm not sure this is a good practice. 但是我不确定这是一个好习惯。 Is there a better way to do so? 有更好的方法吗?

To generate a user and password, you'll have to implement this yourself, as stated by @Eugene S. 要生成用户名和密码,您必须自己实现,如@Eugene S所述。

To share variables between steps, it is best to use Dependency Injection. 要在步骤之间共享变量,最好使用依赖注入。 Cucumber-jvm supports multiple DI frameworks, as listed here: https://cucumber.io/docs/reference/java-di Unfortunately there isn't a lot of "official" (Cucumber) documentation on how to do this. Cucumber-jvm支持多种DI框架,如下所示: https : //cucumber.io/docs/reference/java-di不幸的是,关于如何执行此操作的“官方”(Cucumber)文档并不多。

Thomas Sundberg has written several blog posts on how to share variables between steps using different DI frameworks, including: 托马斯·桑德伯格(Thomas Sundberg)写了几篇博客文章,介绍如何使用不同的DI框架在步骤之间共享变量,包括:

Random user and password is not really necessary. 随机用户和密码并不是真正必要的。 Read please Equivalence Class Testing vs. Boundary Value Testing 请阅读等效类测试与边界值测试

You can create your wildcards for variables: 您可以为变量创建通配符:

`And user enters username and password "VAR#:username" "VAR#:password"`

`@Given("^user enters username and password$")
public void getCredentials(String userName, String password) {
    userName = DataStore.getValue(userName);
    password = DataStore.getValue(password);
}`

Inside dataStore you can get values from property file or anything else. 在dataStore内部,您可以从属性文件或其他任何文件中获取值。

property file can look like: 属性文件如下所示:

VAR#:username=Username VAR#:password=1234 VAR#:用户名=用户名VAR#:密码= 1234

No, Cucumber does not allow anything like that. 不,黄瓜不允许这样的事情。 The only thing Cucumber does, is parse your steps and run the matching methods. Cucumber唯一要做的就是解析您的步骤并运行匹配方法。

To achieve what you describe you can just create a method that will somehow encapsulate data and variable that you require. 要实现您所描述的内容,您只需创建一个将以某种方式封装所需数据和变量的方法。 Then you can run this step in order to obtain it. 然后,您可以运行此步骤以获取它。

For example take the step that you described: 例如,采取您描述的步骤:

And user enters username and password

Then the method that will be executed will look something like: 然后将要执行的方法如下所示:

@Given("^user enters username and password$")
public void getCredentials() {

    //for example
    userName = SomeStaticClaass.getUsername();
    password = SomeStaticClaass.getPassword();

}
@Given("^user enters username and password$")
public void getCredentials() {
    // for example
    userName = SomeStaticClaass.getUsername();
    password = SomeStaticClaass.getPassword();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM