当我使用maven-surefire使用POM文件执行testng.xml时,TestNG测试没有运行

时间:2015-04-09 14:10:09

标签: maven selenium-webdriver testng pom.xml maven-surefire-plugin

当我使用Maven-surefire使用POM文件执行testng.xml时,

0测试正在运行。

我使用testng.xml运行了很少的selenium测试。 当我将testng.xml文件作为TestNG test Suite运行时,它运行正常。

但是,当我包含testng.xml文件(如下所示)时,它没有运行:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.14.1</version>
    <configuration>
        <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
        </suiteXmlFiles>
    </configuration>
</plugin>

以下是我的POM文件摘要:

    <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>SAPAutomation</groupId>
    <artifactId>SAPAutomation</artifactId>
    <!-- <version>3.2</version> -->
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>SAPAutomation</name>
    <url>http://maven.apache.org</url>

    <!-- For javadocs -->
    <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.17</version>
            <configuration>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.14.1</version>
            <configuration>
                <suiteXmlFiles>
                    <suiteXmlFile>testng.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>

     <!-- To exclude unwanted Package ex:  com.SAPAutomation.TEMP etc-->
     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <excludes>
            <exclude>**/com/SAPAutomation/Temp/*</exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
    </build>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-picocontainer</artifactId>
            <version>1.1.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.1.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
              <groupId>org.seleniumhq.selenium</groupId>
              <artifactId>selenium-server</artifactId>
              <version>2.45.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-firefox-driver</artifactId>
            <version>2.45.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.11</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.11</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.2</version>
        </dependency>
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.15</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.jtds</groupId>
            <artifactId>jtds</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>com.github.detro.ghostdriver</groupId>
            <artifactId>phantomjsdriver</artifactId>
            <version>1.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.codeartisans.thirdparties.swing</groupId>
            <artifactId>org-openide-util</artifactId>
            <version>8.6.2</version>
        </dependency>


    <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc14-10.2.0.4.0</artifactId>
            <version>10.2.0.4.0</version>
            <scope>system</scope>
            <systemPath>${basedir}/src/lib/ojdbc14.jar</systemPath>
      <!--      <systemPath>${java.home}/lib/ojdbc14.jar</systemPath> -->
      </dependency>

    <dependency>
        <groupId>org.monte.screenrecorder</groupId>
        <artifactId>MonteScreenRecorder</artifactId>
        <version>10</version>
        <scope>system</scope>
        <systemPath>${basedir}/src/lib/MonteScreenRecorder.jar</systemPath>
        <!-- <systemPath>${java.home}/lib/ojdbc14.jar</systemPath> -->
    </dependency>   

<!--        <repositories>
            <repository>
                <id>ojdbc14</id>
                <url>http://www.oracle.com/technetwork/apps-tech/jdbc-10201-088211.html</url>
            </repository>
        </repositories> -->

    </dependencies>
</project>

我的testng.xml文件:

<suite name="Suite" parallel="tests">
        <listeners>
            <listener class-name="com.SAPAutomation.Listners.RetryListener"/>
        </listeners>

  <test name="Test1">
        <parameter name="TestCaseId" value="TC1" />
        <classes>
          <class name="com.xyz.abc.MyAutomationTestByXML"/>
        </classes>
      </test> 

    <test name="Test2">
        <parameter name="TestCaseId" value="TC2" />
        <classes>
          <class name="com.xyz.abc.MyAutomationTestByXML"/>
        </classes>
    </test> 


    <test name="Test3">
        <parameter name="TestCaseId" value="TC3" />
        <classes>
          <class name="com.xyz.abc.MyAutomationTestByXML"/>
        </classes>
    </test> 
</suite>

运行Pom.xml的测试结果:

[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for SAPAutomation:SAPAutomation:jar:0.0.1-SNAPSHOT
[WARNING] 'dependencies.dependency.systemPath' for com.oracle:ojdbc14-10.2.0.4.0:jar should not point at files within the project directory, ${basedir}/src/lib/ojdbc14.jar will be unresolvable by dependent projects @ line 153, column 16
[WARNING] 'dependencies.dependency.systemPath' for org.monte.screenrecorder:MonteScreenRecorder:jar should not point at files within the project directory, ${basedir}/src/lib/MonteScreenRecorder.jar will be unresolvable by dependent projects @ line 162, column 15
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] 
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building SAPAutomation 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ SAPAutomation ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\AutomationWorkSpace\SAPAutomation\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.3:compile (default-compile) @ SAPAutomation ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ SAPAutomation ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.3:testCompile (default-testCompile) @ SAPAutomation ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 35 source files to C:\AutomationWorkSpace\SAPAutomation\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.14.1:test (default-test) @ SAPAutomation ---
[INFO] Surefire report directory: C:\AutomationWorkSpace\SAPAutomation\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.496 sec

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.137 s
[INFO] Finished at: 2015-04-09T15:01:01+00:00
[INFO] Final Memory: 24M/277M
[INFO] ------------------------------------------------------------------------

请告知。

即使在我的POM.xml文件中提供了完整的Testng.xml路径,问题仍然存在。

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.14.1</version>
            <configuration>
                <suiteXmlFiles>
                    <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>

[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for SAPAutomation:SAPAutomation:jar:0.0.1-SNAPSHOT
[WARNING] 'dependencies.dependency.systemPath' for com.oracle:ojdbc14-10.2.0.4.0:jar should not point at files within the project directory, ${basedir}/src/lib/ojdbc14.jar will be unresolvable by dependent projects @ line 154, column 16
[WARNING] 'dependencies.dependency.systemPath' for org.monte.screenrecorder:MonteScreenRecorder:jar should not point at files within the project directory, ${basedir}/src/lib/MonteScreenRecorder.jar will be unresolvable by dependent projects @ line 163, column 15
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] 
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building SAPAutomation 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ SAPAutomation ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\AutomationWorkSpace\SAPAutomation\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.3:compile (default-compile) @ SAPAutomation ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ SAPAutomation ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.3:testCompile (default-testCompile) @ SAPAutomation ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 35 source files to C:\AutomationWorkSpace\SAPAutomation\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.14.1:test (default-test) @ SAPAutomation ---
[INFO] Surefire report directory: C:\AutomationWorkSpace\SAPAutomation\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.501 sec

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.402 s
[INFO] Finished at: 2015-04-14T17:31:11+00:00
[INFO] Final Memory: 22M/167M
[INFO] ------------------------------------------------------------------------

5 个答案:

答案 0 :(得分:1)

我在实践 Maven 与 TestNG 集成以及 Rahul Sir 的视频时遇到了同样的情况(问题)。

如果没有 TestNG.xml,POM.xml 能够在使用 CLI 的“mvn test”命令调用时执行所有测试。

但是,我注意到,如果我只是直接从 testng.xml 运行测试,即“作为 TestNG 套件运行”,则没有运行任何测试用例。

在仔细查看我的测试 java 类时,我发现,为了在我的测试用例(方法)之前使用 @Test 注释,导入库是 --> import org.junit.Test;因为即使是 jUnit 在其库中也有一个 Test 类。

但是由于我们使用的是TestNG,因此我们必须使用这个import --> import org.testng.annotations.Test;

当我们使用项目模板即 maven-archetype-quickstart 模板在 Eclipse 中创建一个 Maven 项目时,它创建了两个并行文件夹结构 -> 一个用于 java(具有对象模型和实用程序),另一个用于测试(以包含测试用例)。所以,默认情况下,有 AppTest.java 类,它使用 jUnit 库进行 @Test 注释。

因此,在 Eclipse 中创建 Maven 项目后,我们应该在 pom.xml 中添加 TestNG 依赖项,以防我们知道将使用 TestNG 来执行。

我可以通过使用 import org.testng.annotations.Test 来解决这个问题;在我的每个测试类中。

TestNG SuiteXML 仅适用于那些 @Test 注释,而 org.testng.annotations.Test 存在于类中。

希望这也能解决您的错误。谢谢。

答案 1 :(得分:0)

在maven-surefire-plugin中,提供从项目下到testng.xml文件的完整路径 - 示例: <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>

答案 2 :(得分:0)

您需要提供mang的testng.xml文件的完整路径才能获取它。

答案 3 :(得分:0)

您可以将testng.xml文件直接放在项目的pom文件下,然后它将执行您的测试套件。

答案 4 :(得分:0)

上述问题已在上述POM.xml中使用更新版本的Maven-Surefire-plugin解决。整个装置过得非常幸福。后!! 感谢