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

 

 

}

 

}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晒酷网-Ray

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值