提问者:小点点

在cucumber特征文件中仅执行@给定一次


我有以下功能,我想在cucumber中测试。但是,我想只处理一次输入文件(下面功能中的@给定)。但是,它似乎每次都在执行@给定步骤。是否可以在下面的功能中只执行一次这个@给定?

@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个答案

匿名用户

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

以下是如何做到这一点:

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        |

并定义@BeforeControlodName如下:

private static String scenarioName;

public className BeforeMethodName(Scenario scene) {

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

//            Implement your logic

        scenarioName = scene.getName()

        }

        return this;
    }

这样,在每个场景之前都会调用BeforeControlodName,但每个场景大纲只会执行一次逻辑。

匿名用户

钩子应该起作用/应该起作用。或者,您可以设置一个布尔标志并检查它。

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

 }
  ...
}