我可以使用 Maven 生成一个 Jar,我可以使用不同的 testng xml 运行 我的不同 Selenium TestNG 测试

Can I use Maven to generate a Jar that i can run my different Selenium TestNG tests using different testng xmls

我可以使用 Maven 生成一个包含我的测试和依赖项的目录,并使用 "java -cp" 从命令行 运行 它们。但是我不能让它在一个 jar 中工作,而不需要重新创建一个 testng main 并且需要传递 testng xmls 等等。认为这是一件简单的事情被忽略了。

从根本上说,我认为这可能是一个问题,您可以创建一个 运行 包含的包依赖项中的 main 的 jar 吗?我可能没有正确询问或陈述,如果是这样,也将不胜感激。

precon、Maven 3.5.4 和 Java 1.8 安装

我已经能够 "copy dependencies" 和 运行 以下常用的 TestNG 命令行:

java -cp %CD%\target\*; org.testng.TestNG TestTest.xml

但这不如把所有东西都装进一个罐子里好。

如果可能,我不想重新创建 TestNG main 并进一步传递 xmls 等,我已经完成了。我只想按原样使用 TestNG 并将其与我的测试打包在一个 jar 中,并且 运行 像这样: java -jar %CD%\target\myproj-0.0.1-jar-with-dependencies.jar TestTest.xml

来自我的 POM 文件 pom.xml

<properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   <skipTests>true</skipTests>
</properties>
<dependencies>
    <!-- https://mvnrepository.com/artifact/org.testng/testng -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.0.0-beta3</version>
        <scope>provided</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.59</version>
        <scope>provided</scope>
    </dependency>

</dependencies>


<build>

    <!-- Source directory configuration -->
    <sourceDirectory>src</sourceDirectory>

    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>org.testng.TestNG</mainClass>
                    </manifest>
                </archive>

            </configuration>
        </plugin> 

            <!-- // Following plugin executes the testng tests  -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.14.1</version>
            <configuration>
                <!-- // Suite testng xml file to consider for test execution  -->
                <suiteXmlFiles>
                    <suiteXmlFile>testtest.xml</suiteXmlFile>
                </suiteXmlFiles>
                  <skipTests>${skipTests}</skipTests>
            </configuration>
        </plugin>
    </plugins>
 </build>

我的testNG xml TestTest.xml

<groups>
    <run>
      <include name="basic"/> 
    </run>      
</groups>

<classes>
  <class name="myproj.TestTest"/>
</classes>

我在 TestTest.java

中的测试
package myproj;

import org.testng.annotations.Test;

public class TestTest{
    @Test(groups = { "basic" })
    public void Test05BasicPASS() {
        System.out.println("This is test 5, Basic Pass");
    }
}

目录结构:

-testproj
   pom.xml
   TestTest.xml
   -src
      -myproj
         TestTest.java

Jars get output to:
-testproj
  -target

在命令行我可以成功运行: mvn package -DskipTests=false 构建,运行 测试并使用 maven

生成 jar

在 运行ning 和 jar 存在之后,我相信我应该能够 运行 以下内容并进行测试 运行: java -jar %CD%\target\myproj-0.0.1-jar-with-dependencies.jar TestTest.xml 但是我得到: 错误:无法找到或加载主 class org.testng.TestNG

最终目标,能够使用 testng 构建项目,运行 我在命令行上使用 testNG xmls 进行测试。

这应该独立于任何 IDE。

如果你想构建如此调用 "fat jar" - a single executable .jar containing all dependencies and invoking TestNG main class I would recommend going for Maven Shade Plugin:

  1. pom.xml

    中删除以下行
    <scope>provided</scope>
    
  2. 添加以下内容Maven Shade plugin definition to create an executable jar:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.1</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <transformers>
                        <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>org.testng.TestNG</mainClass>
                        </transformer>
                    </transformers>
                </configuration>
            </execution>
        </executions>
    </plugin>
    
  3. Package the jar
  4. 完成后,您应该能够以 jar -jar myproj-0.0.1.jar
  5. 的形式调用您的测试

完整 pom.xml 以防万一:

<?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>com.example</groupId>
    <artifactId>myproj</artifactId>
    <version>0.0.1</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <skipTests>true</skipTests>
    </properties>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.0.0-beta3</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>

    </dependencies>


    <build>

        <!-- Source directory configuration -->
        <sourceDirectory>src</sourceDirectory>

        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>org.testng.TestNG</mainClass>
                        </manifest>
                    </archive>

                </configuration>
            </plugin>

            <!-- // Following plugin executes the testng tests  -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.14.1</version>
                <configuration>
                    <!-- // Suite testng xml file to consider for test execution  -->
                    <suiteXmlFiles>
                        <suiteXmlFile>testtest.xml</suiteXmlFile>
                    </suiteXmlFiles>
                    <skipTests>${skipTests}</skipTests>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>org.testng.TestNG</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>