您好,登錄后才能下訂單哦!
TestNG Assert類方法是怎樣的,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
package com.testng.examples;
import org.testng.Assert;
import org.testng.annotations.Test;
public class AssertTest {
@Test
public void test() {
/**
* Assert#assertEquals
*
* 1.assertEquals方法可對java中所有數(shù)據(jù)類型進行斷言比較。
* 2.基本數(shù)據(jù)類型直接進行值比較進行斷言
* 3.包裝類及自定義繼承自O(shè)bject的數(shù)據(jù)類型則使用equals方法進行比較
* 4.Set類型數(shù)據(jù)使用類的equals方法進行比較(Set類已復寫Object的equals方法)
* 5.其他Collection類型數(shù)據(jù),比如List類型數(shù)據(jù),則按順序遍歷所有元素,并使用equals方法進行比較
* 6.數(shù)組類型數(shù)據(jù),遍歷數(shù)組中各元素,并通過元素類型的equals方法進行比較,如果數(shù)組元素為基本數(shù)據(jù)類型則使用值比較
*/
/*
Assert.assertEquals(actual, expected);
Assert.assertEquals(actual, expected, message);
Assert.assertEquals(actual, expected, delta);
Assert.assertEquals(actual, expected, delta, message);
*/
//用于對map數(shù)據(jù)類型進行比較,該方法會對map元素中數(shù)組各元素按順序比較
//Assert.assertEqualsDeep(null, null);
//用于對set數(shù)據(jù)類型進行比較,該方法會遍歷set元素中所有元素,且Set數(shù)據(jù)為數(shù)組類型時,會對數(shù)組各元素按順序比較
//Assert.assertEqualsDeep(actual, expected, message);
String[] a = new String[]{"a3","a1","a2"};
String[] a1 = new String[]{"a3","a1","a2"};
String[] b = new String[]{"a1","a2","a3"};
Assert.assertEquals(a, a1);
Assert.assertNotEquals(a, b);
System.out.println(a.equals(a1));//true
//斷言兩個數(shù)組包含相同元素,并且忽略數(shù)組元素的排列順序
Assert.assertEqualsNoOrder(a, b);
//斷言兩個bool類型數(shù)據(jù)
Assert.assertFalse(false);
Assert.assertTrue(true);
//斷言O(shè)bject類型數(shù)據(jù)是否為null
Assert.assertNull(null);
Assert.assertNotNull(new Object());
//斷言兩個對象是否引用同一個對象
//ssert.assertSame(new Integer(1), new Integer(1));//failed
Assert.assertNotSame(new Integer(1), new Integer(1));//success
//斷言一段可執(zhí)行程序有異常拋出
Assert.assertThrows(()->{throw new RuntimeException();});//success
//Assert.assertThrows(NullPointerException.class, ()->{throw new RuntimeException();}); //failed
//自定義斷言失敗
//Assert.fail("Test execution failed cased by somthing reason.");
}
}
關(guān)于TestNG Assert類方法是怎樣的問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。