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中所有数据类型进行断言比较。

 * 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.");

}


}


猜你喜欢

转载自blog.51cto.com/shareku/2132034