无法找到或加载主类org.testng.TestNG

时间:2015-03-10 12:07:54

标签: java unit-testing testng

我使用testng-6.8.21.jar从以下链接编写测试用例:

http://www.tutorialspoint.com/testng/testng_tutorial.pdf

我能够编译java文件TestNGSimpleTest.java

但是当我尝试使用此命令时:

java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

它说:

  

无法找到或加载主类org.tesng.TestNG

1 个答案:

答案 0 :(得分:0)

您必须提供类路径中jar的完整路径。例如:

java -cp '/path/to/testng-6.8.8.jar' org.testng.TestNG testng.xml

但是testng需要您还必须包含在类路径中的其他依赖项:

\- org.testng:testng:jar:6.1.1:test
   +- junit:junit:jar:3.8.1:test
   +- org.beanshell:bsh:jar:2.0b4:test
   +- com.beust:jcommander:jar:1.12:test
   \- org.yaml:snakeyaml:jar:1.6:test

最简单的方法是使用依赖项管理器。例如,Maven。

简而言之,您需要(不是必需的,但它使一切变得更容易)拥有标准的项目结构:

main-directory
  pom.xml <- File required by maven. It always has this name.
  -src
    -main
      -java <- Place your Java classes here
      -resources <- Place your images, conf files here etc.
    -test
      -java <- Place your java test classes here
      -resources <- Place your test resources here.

然后,通过这个简单的pom.xml,Maven明白你想要testng并下载testNG的依赖项:

<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.mycompany</groupId>
  <artifactId>my-app-name</artifactId>
  <version>1.0.0-SNAPSHOT</version>

  <!-- Declare your dependencies here-->
  <dependencies>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.1.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

</project>

然后启动:

mvn test

如果您想查看依赖项,请使用:

mvn dependency:tree

(这就是我获得前面的依赖树的方式)