java 命令行运行JAVA+Cucumber+JUnit Maven项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39471048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:24:02  来源:igfitidea点击:

Run JAVA+Cucumber+JUnit Maven project in command line

javamavenjunitcucumber

提问by dsbonafe

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.javaclass 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.javais 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.xmlsurefire-plugin configuration.

pom.xmlsurefire-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! And the project will run in Linux in future.

我使用的是 Windows 8.1!并且该项目将来会在 Linux 上运行。

Thanks.

谢谢。

回答by A user

Go to the path where your Pom.xml is situated. Then execute the command below.

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

Command to run maven, cucumber tests from command prompt.

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

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

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

回答by dsbonafe

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. -- Java file should be on /src/test/java and should have the name Steps.java. $ mvn clean install $ mvn -Dtest=RunnerTest test

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

Thanks for all.

谢谢大家。

回答by James McGuigan

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/

回答by balaji bhadre

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"