读书人

Junit4学习小结

发布时间: 2012-09-08 10:48:07 作者: rapoo

Junit4学习总结

Junit4在工作中一直只关注简单的单元测试应用,并没有进行系统一点的了解。这两天在修改测试用例时,对它进行了一些了解。现把自己的想法记录下来,以便加深记忆和日后查看。

?

Junit4.x后的jar包中,存在两类:org.junit.*和junit.framework.*

?

其中,

??? junit.framework.*是Junit4之前的包,需要继承TestCase类实现单元测试类的编写,且每个测试方法都要求以testxxx规则命名,Junit框架才能运行该测试方法。

??? org.junit.*是Junit4之后的包,不再需要继承任何类,只需要使用注释(@Test)即可实现测试方法的自动调用,测试方法命名也自定义,不需再遵守testxxx的命名规则。

?

Junit的各测试方法的执行顺序不是按代码中的顺序执行,顺序无法指定,不同的运行平台执行顺序也会有差异。

?

??? 以下是针对以上两种Junit的使用示例:

?

1.Junit4.x版本以前的测试方法:

static void afterClass() {System.out.println("this method is called only One Time after...");}}

?

?测试结果如下:

this method is called only One Time before...this method is called EveryTime before...0first Test Case...this method is called EveryTime end...0this method is called EveryTime before...1second Test Case...this method is called EveryTime end...1this method is called only One Time after...

?

备注:以上标红的两处,即@BeforeClass,@AfterClass必须是静态方法

其他知识可参考以下两处:

??? JUnit学习笔记:http://wanqiufeng.blog.51cto.com/409430/426763

??? 全面认识JUnit4的新特特征:http://dev.yesky.com/375/2584375.shtml

读书人网 >编程

热点推荐