【发布时间】:2019-06-11 22:12:25
【问题描述】:
我可以使用 Maven 生成一个包含我的测试和依赖项的目录,并使用 "java -cp" 从命令行运行它们。但是我不能让它在一个 jar 中工作,而不需要重新创建一个 testng main 并且需要传递 testng xmls 等等。认为这是一件被忽视的简单事情。
从根本上说,我相信这可能是一个问题,你能创建一个在包含的包依赖项中运行 main 的 jar 吗?我可能没有正确地询问或陈述,如果是这样,我们也将不胜感激。
安装了 precon、Maven 3.5.4 和 Java 1.8
我已经能够“复制依赖项”并运行以下常用的 TestNG 命令行:
java -cp %CD%\target\*; org.testng.TestNG TestTest.xml
但这并不像把所有东西都放在一个罐子里那么好。
如果可能的话,我不想重新创建 TestNG 主程序并进一步传递我已经完成的 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 xmls 运行我的测试。
这应该独立于任何 IDE 工作。
【问题讨论】:
-
为什么不让通过 Maven 运行测试?