要在Java项目中集成Cucumber,您需要遂行以下步骤:
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>6.9.1</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>6.9.1</version>
</dependency>
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
public class MyStepDefinitions {
@Given("I have a cucumber")
public void i_have_a_cucumber() {
// Implement this step
}
@When("I eat the cucumber")
public void i_eat_the_cucumber() {
// Implement this step
}
@Then("I should be full")
public void i_should_be_full() {
// Implement this step
}
}
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/features")
public class RunCucumberTest {
}
在上面的例子中,@CucumberOptions
注解用于指定Cucumber测试特征文件的路径。
my_feature.feature
的特征文件:Feature: Eating cucumbers
Scenario: Eating a cucumber makes you full
Given I have a cucumber
When I eat the cucumber
Then I should be full
这样,您就可以将Cucumber集成到您的Java项目中,并使用它来编写和运行BDD测试。