Maven:mvn test给出错误+在classpath中找不到类

时间:2015-11-09 12:17:07

标签: java eclipse maven build testng

直接从eclipse IDE运行testng.xml,测试套件被执行但是从maven执行testng.xml会出错。

运行mvn compile,它说BUILD SUCCESS。但是在运行mvn test时会出错。

在通过mvn test运行maven脚本时,我收到以下错误:

[INFO]
[INFO] --- maven-compiler-plugin:3.3:testCompile (default-testCompile) @ XXX
--
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.18:test (default-test) @ XXX---
[INFO] No tests to run.
[INFO] Surefire report directory: \XXX\target\surefire-reports
Running TestSuite
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.295 s
[INFO] Finished at: 2015-11-09T17:34:34+05:30
[INFO] Final Memory: 17M/208M

--------------------
-----------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.
18:test (default-test) on project XXX: Exception in provider:
[ERROR] Cannot find class in classpath: com.XXX.tests.XXXX.classtoexecute
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal o
rg.apache.maven.plugins:maven-surefire-plugin:2.18:test (default-test) on project XXX: Exception in provider
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor
.java:212)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor
.java:153)

请帮忙。

下面是POM.xml

<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.xx.tests</groupId>
  <artifactId>XXX</artifactId>
  <version>1.0-SNAPSHOT</version>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.18</version>
        <configuration>
          <forkCount>0</forkCount>
          <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
      </plugin>
    </plugins>
  </build>  
  <dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.46.0</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8.5</version>
    </dependency>
    <dependency>
        <groupId>atu.testng</groupId>
        <artifactId>reports</artifactId>
        <version>5.0</version>
    </dependency>
  </dependencies>
</project>

TestNg就像贝尔沃一样:

&LT;

?xml version="1.0" encoding="UTF-8"?>
<suite name="XXXX" parallel="none" >
    <listeners>
        <listener class-name="atu.testng.reports.listeners.ATUReportsListener"></listener>
        <listener class-name="atu.testng.reports.listeners.ConfigurationListener"></listener>
        <listener class-name="atu.testng.reports.listeners.MethodListener"></listener>
    </listeners>
  <test name="test1" >
    <classes>
      <class name="com.XXX.tests.XXXX.classtoexecute"/>
    </classes>
 </test> 
</suite>

项目结构如下: Project Structure

3 个答案:

答案 0 :(得分:1)

将非maven转换为maven为我的测试项目工作。

right click on project->Configure->Convert it to Maven

答案 1 :(得分:0)

我创建了与您描述的相同的项目结构。按原样复制pom.xml。下面是我为使其执行所需的类而采取的一些步骤。

  1. 我将testng.xml直接放在项目的根文件夹中,即pom.xml的同一级别。
  2. 接下来在testng.xml我写了

    <classes>
          <class name="com.Local.Tests.TestClass"/>
    </classes>
    

    在开头查看Java文件中的包名称。

  3. mvn test运行。

  4. 我可以看到我的硒测试成功执行。

        [INFO] Surefire report directory: D:\workspace\TestngPOM\target\surefire-reports
    Running TestSuite
    Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 18.659 sec 
    

    试一试。

    这就是我的maven项目结构。enter image description here

答案 2 :(得分:0)

我有同样的错误,

将您的src文件夹移至Main&gt;爪哇

它应该是&#34; src > Main > Java&#34; &GT;

com.something.test.something

com.something.test.sometest2

现在在maven中编译/运行