TestNG是一个基于Java的测试框架,它可以与Cucumber结合使用来进行自动化测试。下面是使用TestNG配置Cucumber的步骤:
<dependencies>
<!-- TestNG -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>
<!-- Cucumber -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>7.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Feature: Example Feature
Scenario: Example Scenario
Given I have a calculator
When I add 2 and 3
Then the result should be 5
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
public class StepDefinitions {
private int result;
@Given("I have a calculator")
public void i_have_a_calculator() {
// 初始化计算器
}
@When("I add {int} and {int}")
public void i_add_and(int num1, int num2) {
result = num1 + num2;
}
@Then("the result should be {int}")
public void the_result_should_be(int expected) {
// 断言结果是否符合预期
}
}
import io.cucumber.testng.CucumberOptions;
import io.cucumber.testng.FeatureWrapper;
import io.cucumber.testng.PickleWrapper;
import io.cucumber.testng.TestNGCucumberRunner;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
@CucumberOptions(
features = "path/to/your/feature/files",
glue = "path.to.your.step.definitions"
)
public class TestRunner {
private TestNGCucumberRunner testNGCucumberRunner;
@BeforeClass(alwaysRun = true)
public void setUpClass() {
testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
}
@Test(dataProvider = "scenarios")
public void runScenario(PickleWrapper pickleWrapper, FeatureWrapper featureWrapper) {
testNGCucumberRunner.runScenario(pickleWrapper.getPickle());
}
@DataProvider(name = "scenarios")
public Object[][] scenarios() {
return testNGCucumberRunner.provideScenarios();
}
@AfterClass(alwaysRun = true)
public void tearDownClass() {
testNGCucumberRunner.finish();
}
}
领取专属 10元无门槛券
手把手带您无忧上云