[英]Run JAVA+Cucumber+JUnit Maven project in command line
I am with a small problem here: run this project using MAVEN and Cucumber.我在这里遇到一个小问题:使用 MAVEN 和 Cucumber 运行这个项目。
I'm with the following structure in my MAVEN project:我在我的 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
```
The Runner.java
class is the following: 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 {
}
```
The class MyTestsSteps.java
is something like the following: 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>
```
I try to run:我尝试运行:
```
mvn clean test
```
And it does not works.它不起作用。
I want to run those tests using Maven and know if is possible set the sequence of execution of Cucumber Tests.我想使用 Maven 运行这些测试,并知道是否可以设置 Cucumber 测试的执行顺序。
I have tried to define in @CucumberOptions features parameter, but it did not work!我试图在@CucumberOptions features 参数中定义,但没有用!
```
features = "{featuresFolder/FirstFeature.feature, featuresFolder/SecondFeature.feature}"
```
and和
```
features = {
"featuresFolder/FirstFeature.feature",
"featuresFolder/SecondFeature.feature"
}
```
And tries to do (as recommended in other post here):并尝试这样做(如此处其他帖子中所推荐):
```
<includes>
<exclude>**/*Runner.java</exclude>
</includes>
```
in pom.xml
surefire-plugin configuration.在
pom.xml
surefire-plugin 配置中。
But it did not work too.但它也不起作用。
Someone could help me to run this project using MAVEN command line and Cucumber if possible.如果可能的话,有人可以帮助我使用 MAVEN 命令行和 Cucumber 运行这个项目。
I am using Windows 8.1!我正在使用 Windows 8.1! And the project will run in Linux in future.
并且该项目将来会在 Linux 上运行。
Thanks.谢谢。
Go to the path where your Pom.xml is situated.转到您的 Pom.xml 所在的路径。 Then execute the command below.
然后执行下面的命令。
Command to run maven, cucumber tests from command prompt.从命令提示符运行 maven、cucumber 测试的命令。
mvn clean test -Dcucumber.options="path/to/feature-files --tags @Your_tag" mvn clean test -Dcucumber.options="path/to/feature-files --tags @Your_tag"
I have solved the problem.我已经解决了这个问题。 I remove the surefire plugin, update my project and run: After I have restructured the project:
我删除了surefire插件,更新了我的项目并运行: 在我重组了项目之后:
-- Features file should be on /src/resource/feature. -- 功能文件应该在 /src/resource/feature 上。 -- Java file should be on /src/test/java and should have the name Steps.java.
-- Java 文件应该在 /src/test/java 上并且应该有名字 Steps.java。
$ mvn clean install $ mvn -Dtest=RunnerTest test
Thanks for all.谢谢大家。
Here's an example of how I used CucumberJVM in a small project.这是我如何在一个小项目中使用 CucumberJVM 的示例。
https://github.com/modernmaster/katas/blob/develop/rock-paper-scissors/ https://github.com/modernmaster/katas/blob/develop/rock-paper-scissors/
Worked for me as well after running below command运行以下命令后也为我工作
mvn clean test -Dcucumber.options="src/ressouces/features --tags @@Your_tag" mvn clean test -Dcucumber.options="src/ressouces/features --tags @@Your_tag"
Please note that the below is deprecated.请注意,以下内容已被弃用。 refer here
参考这里
mvn clean test -Dcucumber.options="path/to/feature-files --tags @Your_tag"
The updated equivalent for above is更新后的等价物是
mvn clean test -Dcucumber.feature="path/to/feature-files" -Dcucumber.filter.tags="@Your_tag"
path/to/feature-files
can also contain sub directories with .feature
files.path/to/feature-files
也可以包含带有.feature
文件的.feature
。 Please refer here to explore more on CLI options请参阅此处以了解有关 CLI 选项的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.