如何在黄瓜JVM步骤之间传递变量 [英] How to pass variables between cucumber-jvm steps

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

问题描述

要在步骤之间传递变量,我有步骤方法属于同一类,并使用该类的字段作为传递的信息。

To pass variables between steps I have the step methods belong to the same class, and use fields of the class for the passed information.

这里是一个示例,如下:

Here is an 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 it is a good practice to share information between steps? Or would be better to define the feature as:

Then User with name "TEST" is created successfully


推荐答案

为了在步骤之间共享共同点,需要使用世界。在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.

引用黄瓜的创建者。


世界是双重的:

The purpose of a "World" is twofold:


  1. 场景之间的隔离状态。

  1. Isolate state between scenarios.

在场景中的步骤定义和挂钩之间共享数据。

Share data between step definitions and hooks within a scenario.

此实现的方式是特定于语言的。例如,在ruby中,
步骤定义中的隐式 self 变量指向当前方案的
的World对象。默认情况下,这是
对象的实例,但是如果您使用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中,您有许多(可能是连接的)World对象

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

与Cucumber-Java中的World等效的是带有钩子或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]并使用依赖项
注入[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://cucumber.io/docs/gherkin/step-organization/

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

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

可用的依赖注入模块为:

The available Dependency Injection modules are:


  • 黄瓜picocontainer

  • 黄瓜汁

  • 黄瓜openejb

  • 黄瓜春天

  • 黄瓜焊

  • 黄瓜针

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

此处的原始帖子 https://groups.google.com/forum/#!topic/cukes/8ugcVreXP0Y

希望这会有所帮助。

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

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