关于 test 测试方面 自己的体会
我的海外blog,有兴趣的可以去看看,别忘记点一下 google的广告哦。
在软件开发流程里面,test(测试)属于比较靠后的 步骤,在经过 概要设计,基本设计,详细设计,制造等流程之后,
才会进入的一个 非常重要的软件开发步骤
基本上,测试的人,应该参与了前面几个步骤,应该说 是非常了解 业务逻辑,制造方法,
以及明确的知道自己想要什么结果的人。
如果做不到上面说的几项,基本上测试的人是个没有灵魂的。也是基本上属于 机械的,不知所以然的 危险的测试。
基本上 测试分为几个阶段
1:制造结束初始--单体测试
2:项目某一个模块制造结束初始--统合测试
3:项目结束初始--系统测试
(本文以JUnit单体测试为主要阐述对象)
单体测试
在制造结束初始阶段,每个单独的程序都需要来验证 业务,功能的正确性。
这个时候,基本面 还是以 程序功能是否正确 作为主要对象。
几种测试工具选择
1:基本上的功能都可以通过JUnit来完成功能测试。点击这里下载
2:辅助的 如果操作数据库方面比较多的程序,可以使用DBUnit。点击这里下载
3:文法上的测试工具,可以选择 JTest (收费)。点击这里下载
这里基本上介绍JUnit 4.X和DBUnit 2.0,JTest由于收费,还有设计验证模板都很繁琐,不在这里介绍。
哦,差一点忘记了,还有一个JUnit 扩展包hamcrest,点击这里下载
参考文章:探索 JUnit 4.4 新特性 来自 IBM developerWork china
下面进入正题:
首先,来看一段代码
equalTo方法
使用过JUnit 的大家,都熟悉这个方法,现在JUnit 4.5 +hamcrest 提供了更方便的 静态方法。
下面来看一下如何对 String来进行比较
@Test
public void testAssertThatConcerningString() {
Assert.assertThat("test", Matchers.containsString("es"));
Assert.assertThat("test", Matchers.startsWith("tes"));
Assert.assertThat("test", Matchers.endsWith("st"));
}
下面是数值的验证
@Test
public void testAssertThatConcerningNumber() {
// clothTo()验证有没有误差
Assert.assertThat(1.01d, Matchers.closeTo(1.001d, 0.01d));
Assert.assertThat(0.991d, Matchers.closeTo(1.001d, 0.01d));
// 因为1.01 不在 1.001 +- 0.005 的范围,所以导致失败
// Assert.assertThat(1.01d, Matchers.closeTo(1.001d, 0.005d));
Assert.assertThat(11, Matchers.greaterThan(10));
Assert.assertThat(11, Matchers.greaterThanOrEqualTo(11));
// 也有lessThan(), lessThanOrEqualTo()
}
下面是比较困难的数组系列
@Test
public void testAssertThatConcerningCollection() {
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
Assert.assertThat(list, Matchers.hasItem(2));
// hasItems()方法的参数是可变长度
Assert.assertThat(list, Matchers.hasItems(2));
Assert.assertThat(list, Matchers.hasItems(1, 2, 3));
Integer[] integers = {new Integer(1), new Integer(2)};
Assert.assertThat(integers, Matchers.hasItemInArray(1));
Map<Integer, String> map = new HashMap<Integer,String>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
Assert.assertThat(map, Matchers.hasEntry(1, "one"));
// Map的key和value,都可以用Matcher来比较
Assert.assertThat(map, Matchers.hasEntry(Matchers.equalTo(2), Matchers.equalTo("two")));
}
下面是Bean的验证
@Test
public void testAssertThatConcerningBeans() {
Item item = new Item();
item.setName("test");
Assert.assertThat(item, Matchers.hasProperty("name"));
Assert.assertThat(item, Matchers.hasProperty("name", Matchers.equalTo("test")));
}
下面是Object的验证
@Test
public void testAssertThatConcerningObject() {
Item item = new Item();
item.setName("test");
// 验证第一个参数对象的toString()
Assert.assertThat(item, Matchers.hasToString(Matchers.equalTo(item.toString())));
// 验证Class#isAssignableFrom()
Assert.assertThat(item.getClass(), Matchers.typeCompatibleWith(Serializable.class));
Assert.assertThat(item.getClass(), Matchers.instanceOf(Serializable.class));
Item nullItem = null;
Assert.assertThat(nullItem, Matchers.nullValue());
Assert.assertThat(item, Matchers.notNullValue());
Assert.assertThat(item, Matchers.sameInstance(item));
}
下面是逻辑的验证
@Test
public void testAssertThatConcerningLogic() {
// Matchers.anything()是一直为True
Assert.assertThat("test", Matchers.anything());
// 全部都是True才可以
Assert.assertThat("test", Matchers.allOf(Matchers.equalTo("test"), Matchers.containsString("es")));
// 有一个为True就可以
Assert.assertThat("test", Matchers.anyOf(Matchers.equalTo("xxxx"), Matchers.containsString("es")));
}
下面是完整的代码:
theOthersOfMatchers.java
package com.weity.JUnit4.samples;
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.anything;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.matchers.JUnitMatchers.containsString;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hamcrest.Matchers;
import org.junit.Test;
/**
* JUnit matchers and org.hamcrest.Macthers.java
*
* @author 113414A0006DL
*
* URL:http://www.atmarkit.co.jp/fjava/rensai4/testng01/testng01_1.html
*
*/
public class theOthersOfMatchers {
/**
*
*/
@SuppressWarnings("unchecked")
@Test
public void chkString(){
String testedString = "developerWorks";
String testedValue = "abc";
String expectedValue = "abc";
// is
assertThat( testedString, is( "developerWorks" ) );
// not
assertThat( testedString, not( "BdeveloperWorks" ) );
// containsString
assertThat( testedString, containsString( "developerWorks" ) );
// endsWith
assertThat( testedString, Matchers.endsWith( "developerWorks" ) );
// startsWith
assertThat( testedString, Matchers.startsWith( "developerWorks" ) );
// equalTo
assertThat( testedValue, equalTo( expectedValue ) );
// equalToIgnoringCase
assertThat( testedString, Matchers.equalToIgnoringCase( "developerWorks" ) );
// equalToIgnoringWhiteSpace
assertThat( testedString, Matchers.equalToIgnoringWhiteSpace( "developerWorks" ) );
assertThat("test", Matchers.equalTo("test"));
assertThat("test", Matchers.equalToIgnoringCase("TEST"));
assertThat("test", Matchers.equalToIgnoringWhiteSpace(" test "));
}
/**
*
*/
@SuppressWarnings("unchecked")
@Test
public void chkInt(){
int testedNumber = 1;
// allOf == &&
assertThat( testedNumber, allOf( Matchers.greaterThan(0), Matchers.lessThan(16) ) );
// anyOf == ||
assertThat( testedNumber, anyOf( Matchers.greaterThan(16), Matchers.lessThan(8) ) );
// anything ==true
assertThat( testedNumber, anything() );
// is
assertThat( testedNumber, is( 1 ) );
// not
assertThat( testedNumber, not( 2 ) );
// equalTo
assertThat( testedNumber, Matchers.equalTo(new Integer(1)));
}
/**
*
*/
@SuppressWarnings("unchecked")
@Test
public void chkDouble(){
double testedDouble = 1.0;
// closeTo
assertThat( testedDouble, Matchers.closeTo( 20.0, 0.5 ) );
// greaterThan
assertThat( testedDouble, Matchers.greaterThan(16.0) );
// lessThan
assertThat( testedDouble, Matchers.lessThan (16.0) );
// greaterThanOrEqualTo
assertThat( testedDouble, Matchers.greaterThanOrEqualTo (16.0) );
// lessThanOrEqualTo
assertThat( testedDouble, Matchers.lessThanOrEqualTo (16.0) );
// closeTo()
assertThat(1.01d, Matchers.closeTo(1.001d, 0.01d));
assertThat(0.991d, Matchers.closeTo(1.001d, 0.01d));
//
assertThat(1.01d, Matchers.closeTo(1.001d, 0.005d));
assertThat(11, Matchers.greaterThan(10));
assertThat(11, Matchers.greaterThanOrEqualTo(11));
// lessThan(), lessThanOrEqualTo()
}
/**
* List<Integer> list = new ArrayList<Integer>()
* Map<Integer, String> map = new HashMap<Integer,String>()
* Map<String, String> mapObject = new HashMap<String,String>()
*
*/
@SuppressWarnings("unchecked")
@Test
public void chkArray(){
//collection
/**
* copy from URL:http://d.hatena.ne.jp/jyukutyo/searchdiary?word=Matcher
*/
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
assertThat(list, Matchers.hasItem(2));
// hasItems()
assertThat(list, Matchers.hasItems(2));
assertThat(list, Matchers.hasItems(1, 2, 3));
Integer[] integers = {new Integer(1), new Integer(2)};
assertThat(integers, Matchers.hasItemInArray(1));
Map<Integer, String> map = new HashMap<Integer,String>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
assertThat(map, Matchers.hasEntry(1, "one"));
//
assertThat(map, Matchers.hasEntry(Matchers.equalTo(2), Matchers.equalTo("two")));
/**
* copy from URL:http://d.hatena.ne.jp/jyukutyo/searchdiary?word=Matcher
*/
List<String> lis = new ArrayList<String>();
lis.add("key");
lis.add("value");
lis.add("elemment");
//collection mapObject = new collection();
//Map mapObject = new HashMap(1);
//Map iterableObject = new HashMap(1);
Map<String, String> mapObject = new HashMap<String,String>();
mapObject.put("key", "value");
mapObject.put("ke2", "two");
mapObject.put("ke3", "three");
//String iterableObject = "";
// hasEntry
assertThat( mapObject, Matchers.hasEntry( "key", "value" ) );
/**
* weity on 2008-10-16
*/
// hasItem
//assertThat( iterableObject, Matchers.hasItem ( "element" ) );
// hasKey
//assertThat( mapObject.get(0), Matchers.hasKey ( "key" ) );
// hasValue
//assertThat( mapObject, Matchers.hasValue ( "key" ) );
}
/**
* Bean
*/
@Test
public void testAssertThatConcerningBeans() {
Item item = new Item();
item.setStrName("test");
assertThat(item, Matchers.hasProperty("strName"));
assertThat(item, Matchers.hasProperty("strName", Matchers.equalTo("test")));
}
/**
* Object
*/
@Test
public void testAssertThatConcerningObject() {
Item item = new Item();
item.setStrName("test");
//
assertThat(item, Matchers.hasToString(Matchers.equalTo(item.toString())));
//
assertThat(item.getClass(), Matchers.typeCompatibleWith(Serializable.class));
assertThat(item.getClass(), Matchers.instanceOf(Serializable.class));
Item nullItem = null;
assertThat(nullItem, Matchers.nullValue());
assertThat(item, Matchers.notNullValue());
assertThat(item, Matchers.sameInstance(item));
}
/**
*
*/
@Test
public void testAssertThatConcerningLogic() {
// Matchers.anything()
assertThat("test", Matchers.anything());
//
assertThat("test", Matchers.allOf(Matchers.equalTo("test"), Matchers.containsString("es")));
//
assertThat("test", Matchers.anyOf(Matchers.equalTo("xxxx"), Matchers.containsString("es")));
}
}
package com.weity.JUnit4.samples;
import java.io.Serializable;
public class Item implements Serializable {
public String strName;
public Item(){
}
public String getStrName() {
return strName;
}
public void setStrName(String strName) {
this.strName = strName;
}
@Override
protected Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
return super.clone();
}
@Override
public boolean equals(Object arg0) {
// TODO Auto-generated method stub
return super.equals(arg0);
}
@Override
protected void finalize() throws Throwable {
// TODO Auto-generated method stub
super.finalize();
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return super.hashCode();
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}
}