要在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測試。