Java中可以使用cucumber来进行业务逻辑的测试。可以参考链接:
https://cucumber.io/docs/cucumber/
首先新建一个maven项目:
添加依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hellocucumber</groupId>
<artifactId>hellocucumber</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-bom</artifactId>
<version>7.5.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.9.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
</plugin>
</plugins>
</build>
</project>
再test-java目录下新建包hellocucumber。新建测试配置类:
package hellocucumber;
import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.IncludeEngines;
import org.junit.platform.suite.api.SelectClasspathResource;
import org.junit.platform.suite.api.Suite;
import static io.cucumber.junit.platform.engine.Constants.PLUGIN_PROPERTY_NAME;
@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("hellocucumber")
@ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty")
public class RunCucumberTest {
}
在test-resources下新建文件夹hellocucumber,新建第一个场景测试example.feature:
Feature: An example
Scenario: The example
Given an example scenario
When all step definitions are implemented
Then the scenario passes
新建对应的测试代码:
package hellocucumber;
import io.cucumber.java.en.*;
import org.junit.jupiter.api.Assertions.*;
public class StepDefinitions {
int age;
@Given("an example scenario")
public void anExampleScenario() {
System.out.println("this is the given");
age = 1;
}
@When("all step definitions are implemented")
public void allStepDefinitionsAreImplemented() {
if (age == 1) {
age = 2;
} else {
age = 3;
}
}
@Then("the scenario passes")
public void theScenarioPasses() {
if (age == 2) {
System.out.println("ok");
} else {
System.out.println("error");
}
}
}
PS:
如果大家使用IDEA,可以使用插件,cucumber-for-java 来辅助。实际的项目中,我们使用的是cucumber-js,而不是cucumber-java,因为java写这个确实啰嗦,而且写完的代码是需要给测试review的,所以选择使用js写。
可以参考官方的入门文档:
https://cucumber.io/docs/guides/10-minute-tutorial/?lang=java