簡體   English   中英

"Maven 項目:能夠使用 TestNG 測試單獨運行單個測試,但無法使用 maven 測試運行整個項目"

[英]Maven Project : Able to run single test separately with TestNG test but unable to run whole project with maven test

我有 Maven 項目,所有配置都是正確的,最近我用 GIT 和 Jenkins 配置了項目,創建了 Jenkins 作業。

早些時候,我能夠通過右鍵單擊項目並運行 **--> **Run --> Maven 測試和測試執行來運行完整的項目,但現在它沒有拋出任何錯誤,但沒有啟動瀏覽器執行。

但是,如果我使用 TestNG 單獨運行 java 文件 [單個測試用例],它按預期工作 [啟動瀏覽器並開始執行]

因為我已經配置了 Jenkins,所以我不能單獨運行每個測試用例。 我將不得不使用Maven 測試運行該項目

我已經嘗試了可能的解決方案:

  1. 清理項目 + 刪除 m2 文件夾 + Maven 運行 [只是為了確保它沒有指向任何舊的 jar 文件] --> 對我不起作用

請找到我在pom.xml中使用的代碼片段

 

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

提前感謝所有建議:)

為了找出原因,您可以按照以下步驟進行操作。 您會發現真正的原因在哪里,它會被卡住。

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>
  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 OR(更加靈活,因為您可以創建指向不同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-testsmvn clean test

Maven的來源

xml的來源

如果您的 testng.xml 或帶有測試用例的 xml 文件位於項目的根目錄中,您可以嘗試以下命令。

  1. 這將觸發 testng.xml 中存在的所有測試用例

  2. 這將執行單個測試用例

    請不要那樣做,您需要在 pom.xml 中添加 surefire 插件。

    "

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM