2014-02-23 51 views
1

我有以下功能,我想在黃瓜中進行測試。但是,我只想處理輸入文件一次(@Given在下面的功能)。但是,它似乎每次都在執行@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 | 

回答

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

} 
    ... 
}