在黄瓜jvm步骤之间传递变量的好习惯 [英] Good practice to pass variables between cucumber-jvm steps

查看:216
本文介绍了在黄瓜jvm步骤之间传递变量的好习惯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在步骤之间传递变量,现在我正在做的例子如下:

To pass variables between steps now I'm doing something like the example as follows:

Feature: Demo

  Scenario: Create user
    Given User creation form management
    When Create user with name "TEST"
    Then User is created successfully

具有步骤定义的Java类:

Java class with steps definitions:

public class CreateUserSteps {

   private String userName;

   @Given("^User creation form management$")
   public void User_creation_form_management() throws Throwable {
      // ...
   }

   @When("^Create user with name \"([^\"]*)\"$")
   public void Create_user_with_name(String userName) throws Throwable {
      //...
      this.userName = userName;
   }

   @Then("^User is created successfully$")
   public void User_is_created_successfully() throws Throwable {
      // Assert if exists an user with name equals to this.userName
   }

我的问题是如果这是一个好的做法,在步骤之间共享信息或者更好地定义功能为:

My question is if this is good practise to share information between steps? Or would be better define the feature as:

Then User with name "TEST" is created successfully

我刚刚使用cucumber-jvm很抱歉if

I'm new with cucumber-jvm so sorry if it's a brainless question.

推荐答案

p>为了分享步骤之间的共性,您需要使用世界。在Java中,它不像Ruby中那么清楚。

In order to share commonalities between steps you need to use a World. In Java it is not as clear as in Ruby.

引用Cucumber的创建者。

Quoting the creator of Cucumber.


世界的目的是双重的:

The purpose of a "World" is twofold:

1)在各个场景之间隔离状态。

1) Isolate state between scenarios.

2)在脚本中的步骤定义和钩子之间共享数据。

2) Share data between step definitions and hooks within a scenario.

这是如何实现的具体语言。例如,在ruby中,
在步骤定义中的隐式 self 变量指向
当前场景的World对象。这是默认的
Object的实例,但是如果你使用World钩子,它可以是任何你想要的。

How this is implemented is language specific. For example, in ruby, the implicit self variable inside a step definition points to the current scenario's World object. This is by default an instance of Object, but it can be anything you want if you use the World hook.

在Java中,你有很多连接)世界对象。

In Java, you have many (possibly connected) World objects.

与Cucumber-Java中的World相当的是所有的对象
with hook或stepdef注释 。换句话说,用@Before,@After,@Given等注释的
方法的任何类都将
实例化为每个场景一次。

The equivalent of the World in Cucumber-Java is all of the objects with hook or stepdef annotations. In other words, any class with methods annotated with @Before, @After, @Given and so on will be instantiated exactly once for each scenario.

这实现了第一个目标。要实现第二个目标,您有两种
方法:

This achieves the first goal. To achieve the second goal you have two approaches:

a)对所有步骤定义和钩子使用单个类

a) Use a single class for all of your step definitions and hooks

b)使用几个类除以责任[1],并使用依赖关系
injection [2]将它们彼此连接。

b) Use several classes divided by responsibility [1] and use dependency injection [2] to connect them to each other.

选项a)快速崩溃,因为您的步骤定义代码
变得混乱。这是为什么人们倾向于使用b)。

Option a) quickly breaks down because your step definition code becomes a mess. That's why people tend to use b).

[1] https://github.com/cucumber/cucumber/wiki/Step-Organization

[2] PicoContainer,Spring,Guice,Weld,OpenEJB, Needle

[2] PicoContainer, Spring, Guice, Weld, OpenEJB, Needle

可用的依赖注入模块是:

The available Dependency Injection modules are:


  • cucumber-picocontainer


  • cucumber-openejb

  • li>
  • 黄瓜焊缝

  • 黄瓜针

  • cucumber-picocontainer
  • cucumber-guice
  • cucumber-openejb
  • cucumber-spring
  • cucumber-weld
  • cucumber-needle

在这里张贴 https://groups.google.com/forum/#!topic/cukes/8ugcVreXP0Y

希望这有助。

这篇关于在黄瓜jvm步骤之间传递变量的好习惯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆