gpt4 book ai didi

java - 在cucumber特征文件中仅执行一次@Given

转载 作者:太空宇宙 更新时间:2023-11-04 07:01:59 26 4
gpt4 key购买 nike

我想在 cucumber 中测试以下功能。但是,我只想处理输入文件一次(以下功能中的@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 和 After Hook ,但没有成功。

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

  @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 |

最佳答案

在每个场景集场景大纲之前运行一些步骤(后台)也可以通过创建标记的@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 将在每个场景之前被调用,但每个场景大纲仅执行一次逻辑。

关于java - 在cucumber特征文件中仅执行一次@Given,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21964962/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com