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

时间:2019-06-11 22:12:25

标签: maven selenium jar testng

我可以使用Maven生成包含我的测试和依赖项的目录,并使用“ java -cp”从命令行运行它们。但是我不能在没有重新创建testng main并需要在其中传递testng xml的情况下在单个jar中工作。认为这是一件容易被忽视的事情。

从根本上讲,我认为这可能是一个问题,您是否可以创建一个在包含的包依赖项中运行main的jar?我可能并没有正确地询问或说明,如果可以的话,也可以提供一些帮助。

已安装precon,Maven 3.5.4和Java 1.8

我已经能够“复制依赖项”并运行以下常用的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软件包-DskipTests = false 使用Maven构建,运行测试并生成jars

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

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

这应该独立于任何IDE进行。

1 个答案:

答案 0 :(得分:0)

如果您要构建一个如此调用的"fat jar"-包含所有依赖项并调用TestNG主类的单个可执行文件.jar,我建议您使用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的身份调用测试

完整的 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>