testNG断言

https://junit.org/junit4/javadoc/latest/org/junit/Assert.html#assertThat

断言:Hamcrest - Matchers

对象:

equalTo - 测试对象相等使用Object.equals方法

hasToString - 测试Object.toString方法

instanceOf,isCompatibleType -测试类型 不会,没有验证

notNullValue,nullValue -测试Null

sameInstance - 测试对象示例

集合:

hasEntry,hasKey,hasValue -测试一个map包含一个实体,键或者值

hasItem,hasItems -测试一个集合包含一个元素

hasItemInArray - 测试一个数组包含一个元素

数字:

closeTo - 测试浮点值接近给定的值

文本:

equalToIgnoringCase - 测试字符串相等忽略大小写

equalToIgnoringWhiteSpace - 测试字符串忽略空白

containsString,endsWith,startsWith - 测试字符串匹配

#java
package com.ali; import com.sun.prism.PhongMaterial;
import org.hamcrest.CoreMatchers;
import org.hamcrest.Matchers;
import org.testng.annotations.Test; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertThat; public class LessonAssert {
@Test
public void testInt(){
//数字
Integer i = 1 + 1;
assertThat(i, is(2));
assertThat(i, equalTo(2));
assertThat(i, not(3));
assertThat(i, allOf(greaterThan(1),lessThan(5)));
assertThat(i, anyOf(greaterThan(1),lessThan(0)));
assertThat(i, anything("始终为true"));
assertThat(i, notNullValue());
assertThat(i, sameInstance(2));
}
@Test
public void testDouble(){
//浮点数
Double a = 12.3;
assertThat(a, closeTo(12,0.5));
assertThat(a, greaterThan(11.0));
assertThat(a, lessThan(12.5));
assertThat(a, greaterThanOrEqualTo(12.3));
assertThat(a, lessThanOrEqualTo(12.5)); }
@Test
public void testString(){
//字符串
String a = "HelloWorld";
assertThat(a, equalTo("HelloWorld"));
assertThat(a, startsWith("He"));
assertThat(a, endsWith("ld"));
assertThat(a, containsString("oW"));
assertThat(a, equalToIgnoringCase("helloworld"));
assertThat(a, equalToIgnoringWhiteSpace(" HelloWorld "));
}
@Test
public void testList(){
//集合
List<String> list = new ArrayList<String>();
list.add("hello");
list.add("world");
assertThat(list,hasItem("hello"));
assertThat(list,hasItems("hello","world"));
}
@Test
public void testmap(){
//map
Map<String,String> a = new HashMap<String,String>();
a.put("阿里","马云");
a.put("腾讯","马化腾");
assertThat(a, hasKey("阿里"));
assertThat(a, hasValue("马云"));
assertThat(a, hasEntry("阿里","马云"));
assertThat(a.get("阿里"), equalTo(""马云""));
}
@Test
public void testList2(){
//数组
String[] a = {"a","b","c","d"};
assertThat(a,hasItemInArray("a")); }
}
上一篇:Windows Phone 十八、加速计


下一篇:TestNG入门到...