我有一个带有LeanFT框架的IntelliJ IDEA测试套件。我使用mvn install命令生成jar文件。当我试图执行它时,我会收到错误消息:
错误:找不到或加载主类test.LeanFTest
LeanFTest类存在于名为package的测试中,并包含以下行:
public class LeanFTest {
public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
TestNG testNG = new TestNG();
testNG.setTestSuites(Arrays.asList("testng.xml"));
testNG.setPreserveOrder(true);
testNG.run();
}testng.xml文件:
<suite name="MyTestSuite" verbose="2" parallel="methods" thread-count="1">
<listeners>
<listener class-name="utils.TestNGListener"></listener>
</listeners>
<test name="Regression" parallel="false" >
<classes>
<class name="test.RegressionTest" />
</classes>
</test>
</suite>我在pom.xml中使用以下插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteIfNewer>true</overWriteIfNewer>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>test.LeanFTest</mainClass>
</manifest>
</archive>
<excludes>
<exclude>log4j2.xml</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>${project.artifactId}-${project.version}</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/leanft-assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>leanft-assembly.xml文件:
<?xml version="1.0"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>fat-tests</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>test</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.directory}/test-classes</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.class</include>
</includes>
<useDefaultExcludes>true</useDefaultExcludes>
</fileSet>
</fileSets>
</assembly>更新:我检查了JAR文件,发现它包含来自/src/main/项目文件夹的文件,但不包含测试类所在的/src/test/文件夹。
更新06.27.我的项目结构与本示例项目中的https://github.com/kohli-harshit/leanft-testng-template相同
发布于 2018-06-25 06:32:57
@Test annotation和必须包含LeanFTest class,而不是Java方法。TestNG不会调用java方法。因此,无论您想要调用哪个类(RegressionTest),都要确保它有要调用的@Test注释。
您可以通过使用TestNG运行来检查脚本,而无需从.XML文件中执行。
发布于 2018-06-26 04:28:35
默认情况下,在构建jar时,驻留在src/test/java下的类从未作为jar的一部分包含。
因此,当您尝试通过jar调用TestNG时,TestNG显然无法找到它。
请参阅this Stackoverflow answer了解如何构建包含测试类的jar。
当您使用IDE时,TestNG将对src/main/java和src/test/java下的所有类具有可见性,这解释了为什么在IDE中工作。
更新:我已经用引用更新了答案,找到了一个正确的堆栈溢出答案,它解决了这个问题。
我正在复制相关章节,供您参考。
pom文件中对应的maven-jar-plugin和maven-assembly-plugin描述。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>org.testng.TestNG</mainClass>
</manifest>
</archive>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/test/resources/assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>文件src/test/resources/assembly.xml的内容
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>test-jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<!-- we're creating the test-jar as an attachement -->
<useProjectAttachments>true</useProjectAttachments>
<unpack>true</unpack>
<scope>test</scope>
</dependencySet>
</dependencySets>
</assembly>现在,当您运行mvn clean package时,应该能够在创建的jar中找到您的测试类。这里我们基本上做了两件事:
maven-jar-plugin,我们首先创建一个包含测试文件的jar (您应该看到target/*-tests.jar下面的jar )maven-assembly-plugin,我们现在正在创建一个uber jar,它也将test-jar作为附件,但在解压缩之后包含它。https://stackoverflow.com/questions/50983746
复制相似问题