如何以程式設計方式關閉TestNG的預設報告器?


TestNG允許從IntelliJ IDE以及命令列執行測試套件。當用戶從IDE或命令列執行testng.xml時,TestNG會生成預設報告。它將所有報告和相應的HTML檔案儲存在**專案->test-output**資料夾中。如果此資料夾不存在,則TestNG會建立一個。

要以程式設計方式停用預設報告,應透過命令列(cmd)執行TestNG。

以下是從命令列執行測試套件的先決條件:

  • 應建立testng.xml檔案來定義要執行的測試套件和測試類。

  • 所有相關的**jar**檔案都應該在專案資料夾中可用。這包括**testing.jar**、**jcommander.jar**以及測試用例中使用的任何其他**jar**檔案。

  • 編譯後儲存. **class** 檔案的**bin**或**out**資料夾的**路徑**。

解決此問題的方法/演算法:

  • **步驟1** - 建立不同的測試類,每個類都有不同的@Test方法和一個@BeforeSuite方法。

  • **步驟2** - 編譯類。這將在IntelliJ中建立**out**資料夾,在Eclipse中建立**bin**資料夾。

  • **步驟3** - 將所有**jar**檔案放在**lib**資料夾中。

  • **步驟4** - 現在建立如下所示的testng.xml。

  • **步驟5** - 開啟**cmd**。

  • **步驟6** - 使用cd <**project_path**>導航到專案路徑。

  • **步驟7** - 執行以下命令

java -cp <path of lib;> <path of out or bin folder> org.testng.TestNG <path of testng>/testng.xml

示例

以下程式碼顯示如何在執行時停用預設報告生成:

src/ NewTestngClass.java

import org.testng.TestNG;
import org.testng.annotations.*;
public class NewTestngClass {
   // test case 1
   @Test()
   public void testCase1() throws InterruptedException {
      System.out.println("in test case 1 of NewTestngClass");
   }
   // test case 2
   @Test()
   public void testCase2() throws InterruptedException {
      System.out.println("in test case 2 of NewTestngClass");
   }
   @BeforeSuite
   public void name() {
      TestNG myTestNG = new TestNG();
      myTestNG.setUseDefaultListeners(false);
      System.out.println("Default reports are disabled");
   }
}

src/testng.xml

這是一個用於組織和執行TestNG測試用例的配置檔案。當只需要執行有限的測試而不是完整的套件時,它非常方便。

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Suite1">
   <test name = "test1">
      <classes>
         <class name = " NewTestngClass"/>
      </classes>
   </test>
</suite>

執行命令:

java -cp .\lib\*;.\out\production\TestNGProject org.testng.TestNG src\testng.xml

輸出

Default reports are disabled
in test case 1 of NewTestngClass
in test case 2 of NewTestngClass

===============================================
Suite1
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

更新於:2022年3月9日

435 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始
廣告