1 修改%MVN_HOME%\conf\settings.xml
找到<mirror>处,加入:
代码语言:javascript
复制
<mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror>
2 在工作目录运行
代码语言:javascript
复制
>mvn archetype:generate "-DarchetypeGroupId=io.cucumber" "-DarchetypeArtifactId=cucumber-archetype" "-DarchetypeVersion=4.2.6.1" "-DgroupId=hellocucumber" "-DartifactId=hellocucumber" "-Dpackage=hellocucumber" "-Dversion=1.0.0-SNAPSHOT" "-DinteractiveMode=false" "-DarchetypeCatalog=local"
形成hellocucumber项目。
3 打开Eclipse,“帮助-eclipse市场”,安装cucumber
4 修改pom.xml
代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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> <cucumber.version>4.2.6</cucumber.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-java</artifactId> <version>${cucumber.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-junit</artifactId> <version>${cucumber.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <encoding>UTF-8</encoding> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
5 书写feature文件。test.feature
代码语言:javascript
复制
#language:zh-CN 功能: 作为一名银行客户 我想要拥有一个账户 以便我可以存钱、取钱,并且显示当前余额 场景:我没有账户 假设我没有账户 当我新建账户的时候 那么我的账户余额为0.00元 场景:我已经有了账户 假设我的账户初始余额为0.00元 当我存入100.00元后 那么我的账户余额为100.00元 假设我的账户初始余额为100.00元 当我存入200.00元后 那么我的账户余额为300.00元 假设我的账户初始余额为400.00元 当我取出300.00元后 那么我的账户余额为100.00元
#language:zh-CN 表示用中文写
6 根据test.feature写步骤文件Stepdefs.java
代码语言:javascript
复制
package hellocucumber; import cucumber.api.java.zh_cn.*; import static org.junit.Assert.*; public class Stepdefs { private Account account; @假如("我没有账户") public void 我没有账户() { this.account = null; } @当("我新建账户的时候") public void 我新建账户的时候() { this.account = new Account(); } @那么("我的账户余额为{double}元") public void 我的账户余额为(Double value) { assertEquals(value, this.account.getBalance()); } @假设("我的账户初始余额为{double}元") public void 我的账户初始余额为_元(Double value) { this.account = new Account(); this.account.deposit(value); } @当("我存入{double}元后") public void 我存入_元后(Double value) { this.account.deposit(value); } @当("我取出{double}元后") public void 我取出_元后(Double value) { this.account.withdraw(value); } }
注意,空格都必须一致
7 修改RunCucumberTest.java文件
代码语言:javascript
复制
package hellocucumber; import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucumber; import org.junit.runner.RunWith; @RunWith(Cucumber.class) @CucumberOptions(features = "src/test/java/hellocucumber/test.feature",plugin = {"pretty"}) public class RunCucumberTest { }
features = "src/test/java/hellocucumber/test.feature"指明feature文件位置
8 修改产品代码
代码语言:javascript
复制
package hellocucumber; public class Account { private Double balance = 0.0; public Double getBalance(){ return this.balance; } public Double deposit(Double value) { return this.balance+=value; } public Double withdraw(Double value) { return this.balance-=value; } }
9 运行
代码语言:javascript
复制
[INFO] Scanning for projects... [INFO] [INFO] --------------------< hellocucumber:hellocucumber >--------------------- [INFO] Building hellocucumber 1.0.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hellocucumber --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ hellocucumber --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hellocucumber --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ hellocucumber --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hellocucumber --- [INFO] Surefire report directory: C:\Code\MyJava\JUnit\hellocucumber\target\surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running hellocucumber.RunCucumberTest 功能: 作为一名银行客户 我想要拥有一个账户 以便我可以存钱、取钱,并且显示当前余额 场景: 我没有账户 # src/test/java/hellocucumber/test.feature:7 假设我没有账户 # Stepdefs.我没有账户() 当我新建账户的时候 # Stepdefs.我新建账户的时候() 那么我的账户余额为0.00元 # Stepdefs.我的账户余额为(Double) 场景: 我已经有了账户 # src/test/java/hellocucumber/test.feature:12 假设我的账户初始余额为0.00元 # Stepdefs.我的账户初始余额为_元(Double) 当我存入100.00元后 # Stepdefs.我存入_元后(Double) 那么我的账户余额为100.00元 # Stepdefs.我的账户余额为(Double) 假设我的账户初始余额为100.00元 # Stepdefs.我的账户初始余额为_元(Double) 当我存入200.00元后 # Stepdefs.我存入_元后(Double) 那么我的账户余额为300.00元 # Stepdefs.我的账户余额为(Double) 假设我的账户初始余额为400.00元 # Stepdefs.我的账户初始余额为_元(Double) 当我取出300.00元后 # Stepdefs.我取出_元后(Double) 那么我的账户余额为100.00元 # Stepdefs.我的账户余额为(Double) 2 Scenarios (2 passed) 12 Steps (12 passed) 0m0.100s Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.237 sec Results : Tests run: 2, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.700 s [INFO] Finished at: 2024-02-01T17:13:15+08:00 [INFO] ------------------------------------------------------------------------