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

TestNG tests are not getting run when I execute testng.xml using POM file using maven-surefire

0 个测试得到 运行,当我使用 Maven-surefire 使用 POM 文件执行 testng.xml 时。

我正在 运行使用 testng.xml 进行一些 selenium 测试。 当我 运行 testng.xml 文件作为 TestNG 测试套件时,它 运行 没问题。

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

<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] ------------------------------------------------------------------------

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

您需要提供 testng.xml 文件的完整路径以便 Maven 获取它。

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

在上述 POM.xml 中使用更新版本的 Maven-Surefire-plugin 解决了上述问题。整个设置从此以后过着幸福的生活! 谢谢

我在使用 TestNG 集成 Maven 以及 Rahul Sir 的视频时遇到了同样的场景(问题)。

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

但是,我注意到,如果我只是 运行 直接从 testng.xml 进行测试,即“运行 作为 TestNG 套件”,那么 none 的测试用例是运行.

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

但是因为我们使用的是TestNG,所以我们必须要使用这个import --> import org.testng.annotations.Test;

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

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

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

TestNG SuiteXML 仅适用于那些 @Test 注释,而 org.testng.annotations.Test 用于 classess。

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