Java Cucumber는 행위 주도 개발(Behavior Driven Development, BDD) 프레임워크로서 테스트 시나리오를 자연어로 작성할 수 있습니다.
Cucumber는 Given, When, Then과 같은 키워드를 사용하여 테스트 스텝을 작성하고 실행할 수 있습니다. 하지만 때로는 동일한 스텝을 여러 번 반복해야 할 때가 있습니다. 이런 경우에는 어떻게 다중 스텝을 호출할 수 있을까요?
Step 클래스 생성하기
먼저, 다중 스텝을 호출하기 위해 별도의 Step 클래스를 생성해야 합니다. 이 클래스에는 다중 스텝을 수행하는 메서드를 작성할 수 있습니다. 예를 들어, multiplyNumbers
라는 메서드를 만들어 두 수를 곱하는 스텝을 수행하도록 할 수 있습니다.
public class StepDefinitions {
public int multiplyNumbers(int a, int b) {
return a * b;
}
}
Scenario에서 다중 스텝 호출하기
Step 클래스를 작성한 후에는 Scenario에서 해당 스텝을 호출할 수 있습니다. 다음은 Cucumber Scenario에서 다중 스텝을 호출하는 예제입니다.
Scenario: Multiply two numbers
Given I have numbers 5 and 3
When I multiply them
Then the result should be 15
위의 Scenario에서 “When I multiply them” 스텝을 다중으로 호출해야 합니다. 이를 위해 다음과 같은 코드를 작성할 수 있습니다.
public class MySteps {
private final StepDefinitions stepDefinitions = new StepDefinitions();
private int result;
@Given("I have numbers {int} and {int}")
public void setNumbers(int a, int b) {
result = stepDefinitions.multiplyNumbers(a, b);
}
@When("I multiply them")
public void multiply() {
// 다중 스텝 호출
result = stepDefinitions.multiplyNumbers(result, 2);
}
@Then("the result should be {int}")
public void checkResult(int expectedResult) {
Assert.assertEquals(expectedResult, result);
}
}
위의 코드에서 multiply
메서드에서 multiplyNumbers
메서드를 다른 스텝에서 호출하는 것을 볼 수 있습니다. 이를 통해 다중 스텝을 실행할 수 있습니다.
이제 Cucumber를 실행하면 “When I multiply them” 스텝이 입력한 숫자를 곱한 후 결과를 체크하는 스텝을 여러 번 실행합니다.
결론
Java Cucumber에서 다중 스텝을 호출하기 위해서는 별도의 Step 클래스를 생성하고 해당 스텝을 호출하는 메서드를 작성해야 합니다. 이를 통해 반복적인 작업을 효율적으로 처리할 수 있습니다.