在黄瓜功能文件中仅执行一次@Given [英] Execute @Given only once in cucumber feature file
问题描述
我要在黄瓜中测试以下功能。但是,我只想处理输入文件一次(以下功能中的@Given)。但是,似乎每次都执行@Given步骤。只能在以下功能中执行一次@Given吗?
I have the following feature that I would like to test in cucumber. But, I would like to process the input file only once ( @Given in the below feature ). But, it seems to be executing the @Given step each time. Is it possible to execute this @Given in the following feature only once?
@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 |
我也尝试了前和后钩子,没有运气就删除了给定步骤。
I also tried Before and After hooks by removing Given step with no luck.
我也在钩子之前尝试过,但对于示例中的每一行,它仍然会循环到此循环。
I also tried before hooks, still it is coming to this loop for each row in the examples.
@Before("@fileValidation")
public void file_is_uploaded() throws Throwable {
String fileName = "something.csv";
processInputFile(fileName);
}
@After("@fileValidation")
public void clear() {
outputFileName = null;
}
在功能文件中我有类似以下内容:
and in the feature file I have something like this:
@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 |
推荐答案
运行一些步骤(背景),也可以通过创建带有 tagged的@Before 方法并将Scenario对象作为参数来实现每个情景集或情景大纲之前。在before方法中,仅当方案名称与上一个方案不同时才执行逻辑。
Running some steps (Background) before each set of scenarios or a Scenario Outline can also be achieved by creating a tagged @Before method and passing a Scenario object as parameter. Within the before method, execute your logic only if the scenario name is different as compared to the last scenario.
下面是操作方法:
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:
And define the @BeforeMethodName as below:
private static String scenarioName;
public className BeforeMethodName(Scenario scene) {
if(!scene.getName().equals(scenarioName)) {
// Implement your logic
scenarioName = scene.getName()
}
return this;
}
这样,在每种情况之前都会调用BeforeMethodName,但是只会执行一次逻辑每个方案大纲。
This way BeforeMethodName will be called before each scenario but will execute the logic only once per Scenario Outline.
这篇关于在黄瓜功能文件中仅执行一次@Given的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!