在黄瓜功能文件中只执行一次@Given

时间:2014-02-23 06:21:59

标签: java cucumber cucumber-junit

我有以下功能,我想用黄瓜测试。但是,我只想处理输入文件一次(@Given在下面的功能)。但是,它似乎每次都在执行@Given步骤。是否可以仅在以下功能中执行此@Given一次?

@fileValidation

Scenario Outline: File Validation

Given a file is uploaded with name "something.csv"
Then response filename created should not match input filename "something.csv"
And reason for errors should be "<Reason>" with error code "<Error code>" for row with RequestId "<RequestId>"

Examples:

  | RequestId     |  Error code | Reason |     
  | 123           |   101       | Failure 1 |
  | 124           |   102       | Failure 1; Failure 2 |

我还尝试通过删除给定步骤而没有运气来尝试前后钩子。

我也在钩子之前尝试过,但是它仍然会在示例中的每一行进入此循环。

  @Before("@fileValidation")
    public void file_is_uploaded() throws Throwable {
        String fileName = "something.csv";
        processInputFile(fileName);
    }

    @After("@fileValidation")
    public void clear() {
        outputFileName = null;
    }

在功能文件中我有这样的东西:

@fileValidation
Scenario Outline: File Validation

Background: Read the uploaded file "something.csv"
Then response filename created should not match input filename "something.csv"
And reason for errors should be "<Reason>" with error code "<Error code>" for row with RequestId "<RequestId>"

Examples:

  | RequestId     |  Error code | Reason |     
  | 123           |   101       | Failure 1 |
  | 124           |   102       | Failure 1; Failure 2 |

2 个答案:

答案 0 :(得分:0)

钩子应该工作/应该有效。或者,您可以设置布尔标志并进行检查。

public class FileValidation {
...
...
private boolean fileOpened = false;

@Given("^a file is uploaded with name \"([^\"]*)\"$")
public void a_file_is_uploaded_with_name(String arg1) throws Throwable {
  if !(fileOpened) {
    processInputFile(...);
    fileOpened = true;
  }

 }
  ...
}

答案 1 :(得分:0)

在每个套方案方案大纲之前运行一些步骤(背景),也可以通过创建一个带有标签的标签来实现@Before 方法并传递一个Scenario对象作为参数。在before方法中,仅当方案名称与上一个方案不同时才执行逻辑。

以下是您的操作方法:

Feature:Setup Data Given Customer logs in as System Admin

@BeforeMethodName
Scenario Outline: Verify ......... 1 
    When <Variable1> And <Variable2> 
    Then <Variable3>

Examples:
    | Variable1 | Variable2 | Variable3 |
    | A1            | B1          | C1        |
    | A2            | B2          | C2        |
    | A3        | B3          | C3        |
    | A4        | B4          | C4        |


@BeforeMethodName
Scenario Outline: Verify ......... 2 
    When <Variable1> And <Variable2> 
    Then <Variable3>

Examples:
    | Variable1 | Variable2 | Variable3 |
    | X1            | Y1          | Z1        |
    | X2            | Y2          | Z2        |
    | X3        | Y3          | Z3        |
    | X4        | Y4          | Z4        |

并按如下所示定义@BeforeMethodName:

private static String scenarioName;

public className BeforeMethodName(Scenario scene) {

        if(!scene.getName().equals(scenarioName)) {

//            Implement your logic

        scenarioName = scene.getName()

        }

        return this;
    }

这样,将在每个方案之前调用BeforeMethodName,但每个方案大纲仅将逻辑执行一次。