【问题标题】:How to call testng.xml file from pom.xml in Maven如何在 Maven 中从 pom.xml 调用 testng.xml 文件
【发布时间】:2012-05-18 23:24:38
【问题描述】:

我正在使用 Selenium WebDriver、Eclipse、TestNG 和 Surefire 插件。我无法从 pom.xml 运行 testng.xml 文件。当我使用 mvn test 运行 pom.xml 时,它直接运行 src/test/java 中的文件。

我的 pom.xml 代码是

<groupId>angel</groupId>
<artifactId>Angel</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

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

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

<dependencies>
          <!-- Java API to access the Client Driver Protocols -->
 <dependency>
     <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>2.21.0</version>
 </dependency>
  <!-- JExcel API is a java library which provides the ability to read, write, and                 modify Microsoft Excel spreadsheets.-->
  <dependency>
     <groupId>net.sourceforge.jexcelapi</groupId>
    <artifactId>jxl</artifactId>
    <version>2.6.10</version>
    </dependency>
   <dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.14</version>
</dependency>
 <!-- Java API for manipulate the Microsoft Excel Sheets.  -->  
 <dependency>  
<groupId>org.apache.poi</groupId>  
<artifactId>poi-contrib</artifactId> 
 <version>3.5-beta5</version>   
 </dependency>
 <!-- Java Mail API used to send Mails. -->   <dependency>             <groupId>javax.mail</groupId> 
 <artifactId>mail</artifactId>   <version>1.4.3</version>
</dependency>
<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>6.3.1</version>
  <scope>test</scope>
</dependency>
 </dependencies>  
  <build>  
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugin</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12</version>
          <configuration>
           <suiteXmlFiles>
               <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
          </suiteXmlFiles>
                 </configuration> 
    </plugin>
 <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
             <source>1.6</source>
            <target>1.6</target>
         </configuration>        </plugin> 

</plugins> 
   </project>

请帮帮我。

我的项目结构是

 project
--src/main/java
--src/main/resources
--src/test/java
    |
     --my testng class file with @Test method.
--src/test/resources
     |
      --testng.xml
--maven dependencies
--jre system library
--src
   |
    --main
    --test
--target
pom.xml

-- =>文件夹名称 |-- =>子文件夹名称

我的 testng.xml 文件是...

  <?xml version="1.0" encoding="UTF-8"?>
  <suite name="Suite1" verbose="1"  >

 <test name="samplePage Test" >
   <classes>
   <class name="TestScripts.SamplePage" >
        <methods>
            <include name = "SamplePageTest_Execution"/>
        </methods>
    </class>
</classes>
 </test>
   <test name="newSamplePage Test" >
    <classes>
   <class name="TestScripts.NewSamplePage" >
        <methods>
            <include name = "NewSamplePage_Execution02"/>
        </methods>
        </class>
    </classes>
   </test> 
 </suite>

我只是想通过 testng.xml 文件从 pom.xml 调用 SamplePage_Execution 方法。

我的 SamplePage_Execution 方法是

  public class sample{
  @Test
  public static void SamplePageTest_Execution() throws Exception{

String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
boolean testStatus = true;
       driver = new FirefoxDriver();
      driver.get("http://www.google.com"); 

   WebElement searchField = driver.findElement(By.name("q")); 

    searchField.sendKeys(new String [] {"selenium2.0"});

    searchField.submit();

    driver.close();
}}

【问题讨论】:

    标签: testng maven-2 pom.xml maven-plugin


    【解决方案1】:

    我在 pom.xml 中的以下代码运行良好:

    <profiles>
       <profile>
          <id>selenium-tests</id>
          <build>
             <plugins>
                <plugin>
                   <groupId>org.apache.maven.plugins</groupId>
                   <artifactId>maven-surefire-plugin</artifactId>
                   <version>2.12.4</version>
                   <configuration>
                      <suiteXmlFiles>
                         <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                      </suiteXmlFiles>
                   </configuration>
                </plugin>     
             </plugins>
          </build>
       </profile>
    </profiles>
    

    使用以下命令运行测试:

    mvn clean test -U -Pselenium-tests
    

    或者,

    mvn clean test
    

    【讨论】:

      【解决方案2】:

      你可以直接通过命令行运行maven测试调用testng.xml

      mvn test -Dsurefire.suiteXmlFiles=src/test/resources/testng.xml
      

      【讨论】:

        【解决方案3】:

        您应该将 suiteXmlFile 从一个类更改为一个文件,而不是例如位于 src/test/resources 中的 testng.xml 文件。此外更新surefire-plugin(当前版本2.12)。另一件事是 selenium 测试通常是集成测试而不是单元测试。

        更新 您必须添加 testng 依赖而不是 junit 依赖。所以删除junit依赖,并根据maven-surefire-plugin的文档添加testng依赖。

        【讨论】:

          【解决方案4】:

          我认为这里有基本的错误.. pom.xml = 由 Maven 维护的包定义

          并且 testing.xml(任何 xml 用户可以定义)用于 TestNG 测试定义。您可以使用 xml 配置,也可以从自定义运行设置中进行自己的配置以运行 Test NG 而且,您在 xml 文件中提供的配置(测试名称、侦听器类名称、套件名称或类名称)应该与您的测试类匹配。就像你已经取消了 class name = TestScripts.SamplePage 但你的 test class= sample 一样,所以这个 xml 永远不会运行你的测试类。 小例子:&lt;class name="com.automation.test.TestA" /&gt; 它在我的测试 xml 中,我的测试类将是 TestA。

          【讨论】:

            【解决方案5】:

            你可以直接给出 testng.xml 而不是 src/test/resources/testng.xml

            我的 POM.xml 工作正常。将其替换为您的 POM.xml 。更新您的 Maven 并再次运行。祝你好运:)

            <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>Automation</groupId>
              <artifactId>Automation</artifactId>
              <version>0.0.1-SNAPSHOT</version>
              <build>
                <plugins>
                  <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3</version>
                    <configuration>
                      <source>1.8</source>
                      <target>1.8</target>
                    </configuration>
                  </plugin>
                  <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.18.1</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>javax.mail</groupId>
                    <artifactId>mail</artifactId>
                    <version>1.5.0-b01</version>
                </dependency>
                <dependency>
                    <groupId>org.testng</groupId>
                    <artifactId>testng</artifactId>
                    <version>6.9.6</version>
                </dependency>
              </dependencies>
            </project>
            

            【讨论】:

              【解决方案6】:
              <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.adiltutorials.facebook</groupId>
                <artifactId>WebdriverTests</artifactId>
                <version>0.0.1-SNAPSHOT</version>
                <build>
                  <plugins>
                    <plugin>
                      <artifactId>maven-compiler-plugin</artifactId>
                      <version>3.3</version>
                      <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                      </configuration>
                    </plugin>
                    <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-surefire-plugin</artifactId>
                      <version>2.19.1</version>
                      <configuration>
                        <forkCount>0</forkCount>
                        <suiteXmlFiles>
                          <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                        </suiteXmlFiles>
                      </configuration>
                    </plugin>
                  </plugins>
                </build>
                <dependencies>
                  <dependency>
                      <groupId>org.seleniumhq.selenium</groupId>
                      <artifactId>selenium-java</artifactId>
                      <version>2.52.0</version>
                  </dependency>
                  <dependency>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
                <version>6.8</version>
              </dependency>  
              </dependencies>
              
              </project>
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-08-20
                • 2022-01-22
                • 2017-08-08
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多