WEB开发网
开发学院WEB开发Jsp Junit 的使用经验总结 阅读

Junit 的使用经验总结

 2008-01-05 08:26:38 来源:WEB开发网   
核心提示:经验一、不要在测试用例的构造函数中做初始化当我们需要增加一个测试时,我们要书写一个自己的测试用例,Junit 的使用经验总结,比如sometest,假如你喜欢在sometest的构造函数中做有关的初始化工作,当然这也有例外,就是基类可以处理任意的测试数据,这可不是个好习惯,如下例:

经验一、不要在测试用例的构造函数中做初始化
当我们需要增加一个测试时,我们要书写一个自己的测试用例,比如sometest。假如你喜欢在sometest的
构造函数中做有关的初始化工作,这可不是个好习惯。如下例:
public class sometest extends testcase{
public sometest(string testname){
super(testname);
//初始化代码
}
}
一旦初始化代码产生异常,比如illegalstateexception,junit随之将产生一个assertionfailederror,
并显示类似下面的出错信息:
junit . framework . assertionfailederror : cannotinstantiatetestcase : test1at
junit . framework . assert . fail ( assert . java : 143 ) at
junit . framework . testsuite$1 . runtest ( testsuite . java : 178 ) at
junit . framework . testcase . runbare ( testcase . java : 129 ) at
junit . framework . testresult$1 . PRotect ( testresult .java : 100 ) at
junit . framework . testresult . runprotected ( testresult. java: 117 ) at
junit . framework . testresult . run ( testresult. java : 103 ) at
junit . framework . testcase . run( testcase . java: 120 ) at
junit . framework . testsuite . run( testsuite . java , compiledcode ) at
junit . ui . testrunner$12 . run (testrunner. java : 429 )
这一大堆出错信息只会让人一头雾水,我们只知道junit无法实例化某个测试用例,到底出了什么问题,在
哪儿出错了呢?不知道!
那么好的做法是怎样呢?
答案是重载测试用例的setup()方法进行初始化。当setup()中的初始化代码产生异常时我们得到的
是类似下面的出错信息:
java . lang . illegalstateexception : oopsatbp . dtc . setup ( dtc .java: 34 ) at
junit . framework . testcase . runbare ( testcase .java: 127 ) at
junit . framework . testresult$ 1 . protect(testresult . java : 100 ) at
junit . framework . testresult . runprotected ( testresult . java: 117 ) at
junit . framework . testresult . run ( testresult .java : 103 )
...
显然这要清楚得多我们一下子就可以知道是在dtc.java 的第34 行产生了illegalstateexception

经验二、不要假定测试用例中测试的执行次序
我们知道在一个junit 的测试用例类中可以包含多个测试,每个测试其实就是一个method。在下面的例子
中有两个不同的测试,尽管testdothisfirst()在位置上先于testdothissecond(),但我们不能就此假定
testdothisfirst()会先执行。
public class sometestcase extends testcase{
public sometestcase(string testname){
super(testname);
}
public void testdothisfirst(){
...
}
public void testdothissecond(){
}
}
由于junit 内部使用一个vector 来存储所有的test,因此在不同的操作系统和java 虚拟机上,test 的执行
次序是不可猜测的。
好的习惯是保持测试之间的独立性,使得它们在任何次序下执行的结果都是相同的。假如真得需要某些测试
按照特定的次序执行,我们可以借助addtest 来实现。如下例:
public static testsuite(){
suite.addtest(new sometestcase(“testdothisfirst”;));
suite.addtest(new sometestcase(“testdothissecond”;));
return suite;
}
这样我们可以确保junit先执行testdothisfirst(),然后执行testdothissecond()。

经验三、测试要避免人工干预
假如某段测试代码需要人工干预,那至少有两个不良后果:一则不能被包括在自动测试中,比如夜间的回
归测试;二则不能被重复执行,例如数据删除的测试不能做完删除就万事大吉,比较好的做法是自动补上
删除掉的数据。经验二讲的是不同的测试要避免相关性,而经验三讲的其实就是测试要避免自相关。
经验四、在子类中调用父类的setup() 和teardown()让我们看一看下面的代码
public class sometestcase extends anothertestcase {
// a connection to a database
private database thedatabase;
public sometestcase (string testname) {
super (testname);
}
public void testfeaturex () {
...
}
public void setup () {
// clear out the database
thedatabase.clear ();
}
}
你发现其中的错误了吗?setup()应该调用super.setup() 以确保anothertestcase 中定义的父类的环境被初
始化了。当然这也有例外,就是基类可以处理任意的测试数据。

进入讨论组讨论。


Tags:Junit 使用 经验

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接