JUnit 4 新特点

转自: http://www.cnblogs.com/deepnighttwo/archive/2011/03/01/1968491.html

编写Testcase

使用JUnit4编写testcase不再有继承Testcase类的负担了。只要在测试方法加annotation @org.junit.Test就行了。

  
  
@org.junit.Test
public void test1(){
// ur test code here
}
一个类可以有多个这种加了@Test annotation标注的测试方法。它们会在跑testcase的时候无序无依赖的跑。

如果一个类中的Test方法需要做一些初始化和清理工作,就需要用到@BeforeClass, @AfterClass, @Before 和@After这几个标注了。

复制代码
  
  
@BeforeClass
public void overallInitial(){
// This method will run ONCE and ONLY ONCE before all test methods in this class. Some initial works should be done here for all test methods in this class.
}

@Before
public void initalTestCase(){
// This method run every time before for a test method. Test method level initial work should be done here.
}

@After
public void clearupTestCase(){
// This method run every time after for a test method. Test method level clearup work should be done here.
}

@AfterClass
public void overallClearup(){
// This method will run ONCE and ONLY ONCE after all test methods in this class. Some clearup works should be done here for all test methods in this class.
}
复制代码

编写testcase基本就这么简单。一切都用标注搞定就行了。

还有一些有用的标注:

复制代码
  
  
忽略某个testcase
@Ignore
( " This case is not supported yet " )

1s内如果不能跑完就算失败
@Test
( timeout = 1000 )

如果抛出IOException则算测试成功
@Test
( expected = IOException . class )
复制代码

组织和运行Testcase

编写完Testcase,一般需要将Testcase组织成Testsuite,这样可以一次跑多个Testcase类。JUnit4中组织Testcase的方式有多种。

通过Annotation

最简单的还是通过annotation。下面的类就是通过Annotation来将多个Testcase组织成一个Suite

复制代码
  
  
package junit.testsuite;

import junit.testcase.JUnitTestCase;
import junit.testcase.TestCase2;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.
class )
@Suite.SuiteClasses({ JUnitTestCase.
class , TestCase2. class })
public class AllTestsUsingAnnotation {

}
复制代码

上面的类不需要代码,就俩标注就行了。一个@org.junit.runner.RunWith,一个@org.junit.runners.Suite。@RunWith表示这个类将以哪种形式来跑。后面的类型必须是Runner接口的实现。在这里指定为Suite。@Suite.SuiteClasses则可以包含多个test unit类。

@Suite.SuiteClasses中的类也可以指定另一个TestSuite,这样就可以有多个包含层次了。不过其中的test unit不能间接或者直接的包含当前类,否则就死循环了嘛。

这个类在Eclipse里面是可以直接Run As JUnit Test的。

通过手工创建TestSuite

如果不使用标注,可以手动创建TestSuite。

复制代码
  
  
package junit.testsuite;

import junit.framework.JUnit4TestAdapter;
import junit.framework.Test;
import junit.framework.TestSuite;
import junit.testcase.TestCase3;

public class AllTestsUsingSuiteMethod {

public static Test suite() {
TestSuite suite
= new TestSuite( " Root Test " );
// $JUnit-BEGIN$
suite.addTest(
new JUnit4TestAdapter(TestCase3. class ));

suite.addTest(
new JUnit4TestAdapter(AllTestsUsingAnnotation. class ));
// $JUnit-END$
return suite;
}

}
复制代码

上面的类创建了一个TestSuite,同时向TestSuite里增加了两个test unit。其中第二个其实就是上面创建的一个TestSuite。

如果在一个TestSuite里面有重复的testcase,那么将只有一个会被运行,重复的将被忽略。

这个类在Eclipse里面是可以直接Run As JUnit Test的。

运行Testcase

除了能够在Eclipse里面运行之外,还可以在普通的程序甚至命令行中跑。

下面的code就是通过应用程序跑TestCase的。根据结果可以生成需要的表格等有组织的报表。

复制代码
  
  
package junit.testsuite;

import junit.testcase.JUnitTestCase;
import junit.testcase.TestCase2;

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class RunTestInMainApp {

public static void main(String[] args) {
Result result
= JUnitCore.runClasses(JUnitTestCase. class ,
TestCase2.
class );
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
}

}
复制代码

在命令行下也可以通过类似的方法跑。

  
  
java org . junit . runner . JUnitCore junit . testcase . JUnitTestCase , junit . testcase . TestCase2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值