使用cucumber运行测试场景并作为Java应用程序生成JVM报告

使用cucumber运行测试场景并作为Java应用程序生成JVM报告,java,cucumber,cucumber-jvm,cucumber-java,cucumber-junit,Java,Cucumber,Cucumber Jvm,Cucumber Java,Cucumber Junit,我正在尝试构建一个java应用程序,在执行命令mvn clean install后复制cucumber的功能。 当前,cucumber在执行mvn clean install后,将执行所有方案并在指定目录中创建JVM报告。 但是,我不想执行mvnclean安装,而是想通过运行我的应用程序jar来复制完全相同的功能。(java应用程序jar中的主方法应该调用cucumber类和方法来执行测试场景并生成报告) 下面是pom.xml和类定义,我正在使用它们运行测试场景,并使用mvn clean ins

我正在尝试构建一个java应用程序,在执行命令
mvn clean install
后复制cucumber的功能。 当前,cucumber在执行
mvn clean install
后,将执行所有方案并在指定目录中创建JVM报告。 但是,我不想执行
mvnclean安装
,而是想通过运行我的应用程序jar来复制完全相同的功能。(java应用程序jar中的主方法应该调用cucumber类和方法来执行测试场景并生成报告)

下面是pom.xml和类定义,我正在使用它们运行测试场景,并使用
mvn clean install

pom.xml

<?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>
    <cucumber.version>6.8.1</cucumber.version>
    <junit.version>4.13</junit.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>${junit.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
  <plugins>  
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2.1</version>
        <executions>
            <execution>
                <id>package-jar-with-dependencies</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.example.mainclass</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </execution>
        </executions>
    </plugin>
        
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
            <encoding>UTF-8</encoding>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>

    <plugin>
        <groupId>net.masterthought</groupId>
        <artifactId>maven-cucumber-reporting</artifactId>
         <version>3.6.0</version>
        <executions>
          <execution>
            <id>execution</id>
            <phase>verify</phase>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <projectName>cucumber-jvm-example</projectName>
              <!-- this is similar report as jenkins report -->
              <outputDirectory>AllReports/cucumber-JVM-reports</outputDirectory>
               <!-- to generate better report-similar to Jenkin report,maven-cucumber-reporting plug-in is used,which reads the generated cucumber.json file in local directory and generates cucumber-JVM-reports -->
              <cucumberOutput>AllReports/report.json</cucumberOutput>
              <skippedFails>true</skippedFails>
              <enableFlashCharts>false</enableFlashCharts>
              <buildNumber>42</buildNumber>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
</build>
StepDefinitions.java

package hellocucumber;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import static org.junit.Assert.*;

class IsItFriday {
    static String isItFriday(String today) {
        return "Friday".equals(today) ? "TGIF" : "Nope";
    }
}

public class StepDefinitions {
    private String today;
    private String actualAnswer;

    @Given("today is {string}")
    public void today_is(String today) {
        this.today = today;
    }

    @When("I ask whether it's Friday yet")
    public void i_ask_whether_it_s_Friday_yet() {
        actualAnswer = IsItFriday.isItFriday(today);
    }

    @Then("I should be told {string}")
    public void i_should_be_told(String expectedAnswer) {
        assertEquals(expectedAnswer, actualAnswer);
    }
}

黄瓜芯上有一个小黄瓜。有关使用说明,请使用:


mvn exec:java                                  \
    -Dexec.classpathScope=test                 \
    -Dexec.mainClass=io.cucumber.core.cli.Main \
    -Dexec.args="--help"
或主要方法:

import io.cucumber.core.cli.Main;

public class Example {
    public static void main(String[] args) {
        byte status = Main.run("--help");
        System.exit(status);
    }

}
import io.cucumber.core.cli.Main;

public class Example {
    public static void main(String[] args) {
        byte status = Main.run("--help");
        System.exit(status);
    }

}