在命令行中运行JAVA + Cucumber + JUnit Maven项目

时间:2016-09-13 13:13:58

标签: java maven junit cucumber

我在这里遇到一个小问题:使用MAVEN和Cucumber运行这个项目。

我在MAVEN项目中使用以下结构:

```
br.com.home.cucumberProjectWithWS
                |--- src/tests/java
                |                             |--- com.home.Model (secret)
                |                             |--- com.home.Control (secret)
                |                             |--- com.home.View
                                               |                             |--- ... (secret)
                                               |                             |--- Runner.java
                |                             |--- com.home.javaStepsFolder
                |                                                             |--- MyTestsSteps.java
                |--- src/main/java
                |--- src/main/resources
                |--- src/tests/resources
                |--- featuresFolder
                |                             |--- FirstFeature.feature
                |                             |--- SecondFeature.feature
                |                             |--- ThirdFeature.feature
                |--- pom.xml
```

Runner.java类如下:

```
package br.com.home.runner;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(monochrome = false, plugin = { "html:target/cucumber-html-report", "json:target/cucumber.json",
                               "pretty:target/cucumber-pretty.txt", "usage:target/cucumber-usage.json",
                               "junit:target/cucumber-results.xml" }, features = "featuresFolder", glue = { "br.com.home.javaStepsFolder" })
public class Runner {

}
```

班级MyTestsSteps.java如下所示:

```
package br.com.home.runner;

import cucumber.api.*;

class MyTestsSteps{

                Scenario scenario;
                Controller controller = new Control();

                @Before
                public void doItBefore(Scenario scenario){
                               this.scenario = scenario;
                }

                @When("^we do something$")
                public void doSomething(){
                               controller.doSomething();
                }

                @When("^we do something else$")
                public void doSomethingElse(){
                               controller.doSomethingElse();
                }

                @Then("^we expect \"([^\"]*)$")
                public void weExpectSomeResult(String result){
                               assertTrue(controller.getResultExpected().equals(result));
                }
}
```

And my `pom.xml` is the following:

```
<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>com.home.cucumberProjectWithWS</groupId>
                <artifactId>com.home.cucumberProjectWithWS</artifactId>
                <version>0.0.1-SNAPSHOT</version>

                <dependencies>
                               <!-- CUCUMBER -->

                               <!-- CUCUMBER: Java -->

                               <dependency>
                                               <groupId>info.cukes</groupId>
                                               <artifactId>cucumber-java</artifactId>
                                               <version>1.2.4</version>
                                               <scope>test</scope>
                               </dependency>

                               <!-- CUCUMBER: Core -->
                               <dependency>
                                               <groupId>info.cukes</groupId>
                                               <artifactId>cucumber-core</artifactId>
                                               <version>1.2.4</version>
                               </dependency>

                               <!-- CUCUMBER: JUnit -->
                               <dependency>
                                               <groupId>info.cukes</groupId>
                                               <artifactId>cucumber-junit</artifactId>
                                               <version>1.2.4</version>
                                               <scope>test</scope>
                               </dependency>

                               <!-- CUCUMBER: JVM Deps -->
                               <dependency>
                                               <groupId>info.cukes</groupId>
                                               <artifactId>cucumber-jvm-deps</artifactId>
                                               <version>1.0.5</version>
                               </dependency>

                               <!-- CUCUMBER: Reports -->
                               <dependency>
                                               <groupId>net.masterthought</groupId>
                                               <artifactId>cucumber-reporting</artifactId>
                                               <version>2.5.0</version>
                               </dependency>

                               <!-- CUCUMBER: Gherkin -->
                               <dependency>
                                               <groupId>info.cukes</groupId>
                                               <artifactId>gherkin</artifactId>
                                               <version>2.12.2</version>
                               </dependency>

                               <!-- MOCKITO: All -->
                               <dependency>
                                               <groupId>org.mockito</groupId>
                                               <artifactId>mockito-all</artifactId>
                                               <version>1.10.19</version>
                               </dependency>

                               <!-- JUNIT -->
                               <dependency>
                                               <groupId>junit</groupId>
                                               <artifactId>junit</artifactId>
                                               <version>4.11</version>
                                               <scope>test</scope>
                               </dependency>
                </dependencies>


                <build>
                               <sourceDirectory>src/main/java</sourceDirectory>
                               <plugins>
                                               <plugin>
                                                               <artifactId>maven-compiler-plugin</artifactId>
                                                               <version>3.3</version>
                                                               <configuration>
                                                                               <source>1.8</source>
                                                                               <target>1.8</target>
                                                                               <encoding>UTF-8</encoding>
                                                               </configuration>
                                               </plugin>

                                               <plugin>
                                                               <groupId>org.apache.maven.plugins</groupId>
                                                               <artifactId>maven-surefire-plugin</artifactId>
                                                               <version>2.19.1</version>
                                                               <configuration>
                                                                               <properties>
                                                                                              <property>
                                                                                                              <name>junit</name>
                                                                                                              <value>true</value>
                                                                                              </property>
                                                                               </properties>
                                                                               <includes>
                                                                                              <include>**/*Runner.java</include>
                                                                               </includes>
                                                               </configuration>
                                               </plugin>

                               </plugins>
                </build>

</project>
```

我试着跑:

```
mvn clean test
```

它不起作用。

我想使用Maven运行这些测试,并知道是否可以设置黄瓜测试的执行顺序。

我试图在@CucumberOptions功能参数中定义,但它不起作用!

```
features = "{featuresFolder/FirstFeature.feature, featuresFolder/SecondFeature.feature}"
```

```
features = {
                "featuresFolder/FirstFeature.feature", 
                "featuresFolder/SecondFeature.feature"
}
```

尝试做(按照其他帖子的建议):

```
<includes>
                <exclude>**/*Runner.java</exclude>
</includes>
```

pom.xml surefire-plugin配置中。

但它也没有用。

如果可能,有人可以帮我使用MAVEN命令行和Cucumber来运行这个项目。

我正在使用Windows 8.1! 该项目将在未来的Linux中运行。

感谢。

5 个答案:

答案 0 :(得分:3)

转到Pom.xml所在的路径。然后执行以下命令。

从命令提示符运行maven,cucumber测试的命令。

mvn clean test -Dcucumber.options =“src / resources / features --tags @@ Your_tag”

答案 1 :(得分:2)

我已经解决了这个问题。 我删除了surefire插件,更新我的项目并运行: 在我重组了项目之后:

- 功能文件应位于/ src / resource / feature。 - Java文件应该在/ src / test / java上,并且应该具有名称Steps.java。 $ mvn clean install $ mvn -Dtest=RunnerTest test

谢谢大家。

答案 2 :(得分:0)

以下是我在一个小项目中使用CucumberJVM的示例。

https://github.com/modernmaster/katas/blob/develop/rock-paper-scissors/

答案 3 :(得分:0)

在执行以下命令后也为我工作

MVN干净测试-Dcucumber.options =“ src / ressouces / features --tags @@ Your_tag”

答案 4 :(得分:0)

请注意,以下内容已被弃用。 refer here

mvn clean test -Dcucumber.options="path/to/feature-files --tags @Your_tag"

更新后的等价物是

mvn clean test -Dcucumber.feature="path/to/feature-files" -Dcucumber.filter.tags="@Your_tag"
  • 注意:path/to/feature-files 还可以包含带有 .feature 文件的子目录。

refer here探索更多 CLI 选项