如何使用TestNG斷言兩個列表相等?


TestNG 支援許多斷言。它具有 **org.testng.Assert** 類,該類擴充套件了 Java 物件類 **java.lang.object**。

為了特別比較兩個列表,TestNG 的 Assert 類有一個稱為 **assertEquals(Object actual, Object expected)** 的方法,並且此方法還有一個帶有自定義訊息的擴充套件版本 **assertEquals(Object actual, Object expected, String message)**。

如果滿足以下條件,此方法將返回 True:

  • 兩個物件都是列表,

  • 兩個列表的大小相同,並且

  • 如果列表的元素順序相同。

如果這些條件中的任何一個不為真,它將返回 False。

在本文中,我們將討論如何在 TestNG 中比較兩個列表。

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

  • **步驟 1** - 建立一個 TestNG 類“**NewTestngClass**”。

  • **步驟 2** - 在類中編寫三個不同的 @Test 方法,如程式設計程式碼部分所示。

    • **第一個 @Test 方法** - 它恰好包含 2 個相同的列表;滿足所有 3 個條件並比較這兩個列表。此測試將透過。

    • **第二個 @Test 方法** - 兩個列表的大小不同,此測試將丟擲異常。

    • **第三個 @Test 方法** - 列表中元素的順序不同,此測試也將丟擲異常。

  • **步驟 3** - 現在建立如下所示的 testNG.xml 來執行 TestNG 類。

  • **步驟 4** - 執行 testNG.xml 或直接在 IDE 中執行 TestNG 類,或者使用命令列編譯並執行它。

示例

對通用 TestNG 類“**NewTestngClass**”使用以下程式碼:

src/ NewTestngClass.java

import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.List;
public class NewTestngClass {
   @Test
   public void testCase1() {
      System.out.println("in test case 1 of NewTestngClass");
      List<String> expectedName = new ArrayList<String>();
      expectedName.add("ramesh");
      expectedName.add("mahesh");
      expectedName.add("suresh");
      List<String> actualName = new ArrayList<String>();
      actualName.add("ramesh");
      actualName.add("mahesh");
      actualName.add("suresh");
      Assert.assertEquals(actualName,expectedName, "Comaparsion of 2 lists");
   }
   @Test
   public void testCase2() {
      System.out.println("in test case 2 of NewTestngClass");
      List<String> expectedName = new ArrayList<String>();
      expectedName.add("ramesh");
      expectedName.add("mahesh");
      expectedName.add("suresh");
      List<String> actualName = new ArrayList<String>();
      actualName.add("ramesh");
      actualName.add("mahesh");
      Assert.assertEquals(actualName,expectedName,"Comparison of 2 lists");
   }
   @Test
   public void testCase3() {
      System.out.println("in test case 3 of NewTestngClass");
      List<String> expectedName = new ArrayList<String>();
      expectedName.add("ramesh");
      expectedName.add("mahesh");
      expectedName.add("suresh");
      List<String> actualName = new ArrayList<String>();
      actualName.add("suresh");
      actualName.add("ramesh");
      actualName.add("mahesh"); Assert.assertEquals(actualName,expectedName,"Comaparsion of 2 lists");
   }
}

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>

輸出

in test case 1 of NewTestngClass
in test case 2 of NewTestngClass

java.lang.AssertionError: Comaparsion of 2 lists: lists don't have the same size expected [3] but found [2]

Expected :3
Actual :2

in test case 3 of NewTestngClass

java.lang.AssertionError: Comaparsion of 2 lists: Lists differ at element [0]: ramesh != suresh expected [ramesh] but found [suresh]
Expected :ramesh
Actual :suresh

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

更新於:2022-03-09

3K+ 次檢視

啟動你的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.