1. 程式人生 > >TestNG Assert類方法詳解

TestNG Assert類方法詳解

類方法 null cep 進行 ots ces 一個 遍歷數組 spa

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中所有數據類型進行斷言比較。

* 2.基本數據類型直接進行值比較進行斷言

* 3.包裝類及自定義繼承自Object的數據類型則使用equals方法進行比較

* 4.Set類型數據使用類的equals方法進行比較(Set類已復寫Object的equals方法)

* 5.其他Collection類型數據,比如List類型數據,則按順序遍歷所有元素,並使用equals方法進行比較

* 6.數組類型數據,遍歷數組中各元素,並通過元素類型的equals方法進行比較,如果數組元素為基本數據類型則使用值比較

*/

/*

Assert.assertEquals(actual, expected);

Assert.assertEquals(actual, expected, message);

Assert.assertEquals(actual, expected, delta);

Assert.assertEquals(actual, expected, delta, message);

*/

// 用於對map數據類型進行比較,該方法會對map元素中數組各元素按順序比較

// Assert.assertEqualsDeep(null, null);

// 用於對set數據類型進行比較,該方法會遍歷set元素中所有元素,且Set數據為數組類型時,會對數組各元素按順序比較

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

// 斷言兩個數組包含相同元素,並且忽略數組元素的排列順序

Assert.assertEqualsNoOrder(a, b);

// 斷言兩個bool類型數據

Assert.assertFalse(false);

Assert.assertTrue(true);

// 斷言Object類型數據是否為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

// 斷言一段可執行程序有異常拋出

Assert.assertThrows(()->{throw new RuntimeException();}); //success

// Assert.assertThrows(NullPointerException.class, ()->{throw new RuntimeException();}); //failed

// 自定義斷言失敗

// Assert.fail("Test execution failed cased by somthing reason.");

}


}


TestNG Assert類方法詳解