要使用Java编写Cucumber测试用例,您需要遵循以下步骤:
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>6.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>6.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.7.2</version>
<scope>test</scope>
</dependency>
</dependencies>
Feature: Calculator
Scenario: Add two numbers
Given I have entered 50 into the calculator
And I have entered 70 into the calculator
When I press add
Then the result should be 120 on the screen
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
public class CalculatorSteps {
private int result;
@Given("I have entered {int} into the calculator")
public void i_have_entered_number_into_the_calculator(int number) {
// Enter number into the calculator
}
@When("I press add")
public void i_press_add() {
// Press add button
result = // Perform addition operation
}
@Then("the result should be {int} on the screen")
public void the_result_should_be_number_on_the_screen(int expected) {
// Verify that the result is equal to the expected value
}
}
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/calculator.feature")
public class TestRunner {
}
这是一个简单的示例,您可以根据您的需求和具体情况进行调整和扩展。希望这些步骤可以帮助您开始使用Java编写Cucumber测试用例。