簡體   English   中英

在黃瓜功能文件中僅執行一次@Given

[英]Execute @Given only once in cucumber feature file

我要在黃瓜中測試以下功能。 但是,我只想處理輸入文件一次(以下功能中的@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 |

我也嘗試通過刪除Give步驟來實現之前和之后的鈎子,但是沒有運氣。

我還在鈎子之前嘗試過,但對於示例中的每一行,它仍然會進入此循環。

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

掛鈎應該工作/應該已經工作。 另外,您可以設置一個布爾標志並進行檢查。

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;
  }

 }
  ...
}

也可以通過創建標記的@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,但每個方案大綱僅執行一次邏輯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM