原因
最近工作,发现写好了功能每次都要点页面来测试功能,然后发现bug再改再测试,这样好繁琐。想一想,我们主要还是对数据进行操作,那么我们直接测试数据的流向,这样可以减轻测试的时间。以前就关注过junit,后来由于公司不使用单元测试,就没有继续研究,最近看了junit in action这本书,发现单元测试非常重要。
简单的junit测试
使用junit网上已经有很多例子,看看demo就可以轻松解决,在这里记录日常工作中使用junit和一些小技巧。
用suite来组合测试
个人理解就是运行多个测试类,比如你在工作中想做一系列的操作,但是每个操作都是独立的dao,那么一个一个的运行test会很繁琐。还好junit提供了suite。
下面我们来提供2个test。分别是NumberTest1与NumberTest2,然后使用NumberSuite来进行组合测试。
package org.csdn.suite.test;
import static org.junit.Assert.*;
import org.junit.Test;
public class NumberTest1 {
/**
* 简单的测试,不涉及任何业务。
*/
@Test
public void number(){
assertEquals(5, 5);
};
}
package org.csdn.suite.test;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class NumberTest2 {
/**
* 简单的测试,不涉及任何业务。
*/
@Test
public void number(){
assertEquals('c', 'c');
};
}
package org.csdn.suite;
import org.csdn.suite.test.NumberTest1;
import org.csdn.suite.test.NumberTest2;
import org.junit.runner.RunWith;
import org.junit.runners.Suite.SuiteClasses;
/**
* 使用suite进行组合测试,目的是想达到numbertest1和numbertest2一起运行。
* @author hxq-game
*
*/
@RunWith(value = org.junit.runners.Suite.class)
@SuiteClasses(value = {
NumberTest1.class,
NumberTest2.class
})
public class NumberSuite {
}
然后将NumberStuie “run as” junit test 会发现出现如下结果:
这样简单的组合测试就完成了。
版权声明:本文为hxq_793034963原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。