【问题标题】:Maven Project : Able to run single test separately with TestNG test but unable to run whole project with maven testMaven 项目:能够使用 TestNG 测试单独运行单个测试,但无法使用 maven 测试运行整个项目
【发布时间】:2019-05-05 12:47:35
【问题描述】:

我有 Maven 项目,所有配置都是正确的,最近我用 GIT 和 Jenkins 配置了项目,创建了 Jenkins 作业。

早些时候,我能够通过以下方式运行完整的项目 右键单击项目并运行 **--> **Run --> Maven 测试 并测试执行用于启动但现在它没有抛出任何错误但没有启动浏览器执行。

但是,如果我使用 TestNG 单独运行 java 文件 [单个测试用例],它会按预期工作[启动浏览器并开始执行]

因为我已经配置了 Jenkins,所以我不能单独运行每个测试用例。 我将不得不使用 Maven 测试

运行该项目

我已经尝试了可能的解决方案:

  1. 清理项目 + 删除 m2 文件夹 + Maven 运行 [只是为了确保它没有指向任何旧的 jar 文件] --> 不适合我

请找到我在pom.xml

中使用的代码sn-p

 

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
            </plugin>
        </plugins>
    </build>

提前感谢所有建议:)

【问题讨论】:

  • 请分享错误详情,Maven 测试在控制台中抛出了什么。
  • @IshitaShah :它没有抛出任何错误消息,但没有启动浏览器,也没有执行任何测试用例。但是如果我们单独运行测试用例,它就会成功执行
  • 如果测试也没有启动,控制台窗口上可能有一些关于 maven 测试状态的信息。我经历过。
  • ----------- 测试 ----------- 运行 TestSuite LoginTest 测试从行开始 - 13 总行是 - 1 总列是 - 4 log4j:WARN 找不到记录器(freemarker.cache)的附加程序。 log4j:WARN 请正确初始化 log4j 系统。 log4j:WARN 请参阅logging.apache.org/log4j/1.2/faq.html#noconfig 了解更多信息。即将从 excel 读取数据 所有数据已从 excel 检索到 LoginTest 测试从行开始 - 25 总行是 - 1 总列是 - 4 LoginTest 测试从行开始 - 9 总行是 - 1 总列是 - 4登录测试
  • @IshitaShah :这是控制台上显示的内容,之后它不会做任何事情。

标签: maven jenkins selenium-webdriver maven-plugin ui-automation


【解决方案1】:

要追查原因,您可以通过以下步骤。你会发现真正的原因在哪里并且它被卡住了。

1.使用控制台输出创建简单的@Test:

public class NewTest {
  @Test
  public void f() {
      System.out.println("Hello World");
  }
}

2。在项目的根位置创建 TestNG.XML 以执行上述类:

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="Suite">
      <test thread-count="5" name="Test">
        <classes>
          <class name="packageName.NewTest"/>
        </classes>
      </test> <!-- Test -->
    </suite> <!-- Suite -->

3.运行并检查 TESTNG.XML 输出

4.运行并检查 POM.XML 的输出

POM.XML:

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

【讨论】:

    【解决方案2】:
    1. 创建一个 testNG xml 并在其中包含所有测试类。这可以通过包含顶级包来完成:

    <test name="sample-Test" preserve-order="true" verbose="2">
        <packages>
            <package name="org.sample.something.*"/>
        </packages>
    </test>
    

    2.1 更新您的surefire插件配置以使其:

            <configuration>
               <suiteXmlFiles>
                   <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
               </suiteXmlFiles>
            </configuration>
    

    2.2 或(更灵活,因为您可以创建指向不同 xml 的不同配置文件以分别运行不同的测试套件)创建一个指向您的 testNG xml 的 maven 配置文件

         <profiles>
           <profile>
              <id>selenium-tests</id>
              <build>
                 <plugins>
                    <plugin>
                       <groupId>org.apache.maven.plugins</groupId>
                       <artifactId>maven-surefire-plugin</artifactId>
                       <version>2.19.1</version>
                       <configuration>
                          <suiteXmlFiles>
                             <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                          </suiteXmlFiles>
                       </configuration>
                    </plugin>     
                 </plugins>
              </build>
           </profile>
        </profiles>
    
    1. mvn clean test -U -Pselenium-tests 或 mvn clean test

    source for maven

    source for xml

    【讨论】:

    • 感谢您提供详细的解决方案,但不幸的是,此解决方案不适用于我。请在下面找到更新的 sn-p:org.apache.maven.pluginsmaven-surefire-plugin2.19.1truesrc/test/resources/testng.xml`
    • 但它仍然抛出错误 [错误] 无法在项目 Sales_Force_Test_Automation 上执行目标 org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (default-test):执行目标org.apache.maven.plugins的默认测试:maven-surefire-plugin:2.19.1:测试失败:分叉进程中出现错误[错误] org.testng.TestNGException:[错误]找不到类在类路径中:com.ef.sl.testcases.Imports.ImportTest
    • @PrateekNeelgund 请在这里查看stackoverflow.com/questions/25543910/…
    • 尝试-> mvn clean install
    • 不走运!!!它没有显示任何错误,单个测试文件执行工作,但是当我选择完整的项目并进行 maven test / maven install 时,它没有启动任何浏览器,也没有显示任何错误
    【解决方案3】:

    如果您的 testng.xml 或带有测试用例的 xml 文件位于项目的根目录中,您可以尝试以下命令。

    1. 这将触发 testng.xml 中存在的所有测试用例

           mvn clean test -Dsurefire.suiteXmlFiles=testng.xml
      
    2. 这将执行单个测试用例

           mvn clean test -Dtest=className
      

    请不要那样做,你需要在 pom.xml 中加入 surefire 插件。

    【讨论】:

      猜你喜欢
      • 2020-11-26
      • 2015-11-18
      • 1970-01-01
      • 1970-01-01
      • 2021-02-18
      • 2010-12-24
      • 2020-02-21
      • 2012-01-02
      相关资源
      最近更新 更多