[Junit]Understanding unit test framework

There are several best practices that unit testing frameworks should follow. These seemingly minor improvements in the TestCalculator program hightlight three rules that(in our experience) all unit testing frameworks should observe:

  • Each unit test must run independently of all other unit tests.
  • Errors must be detected and reported test by test.
  • It must be easy to define which unit tests will run.

The "slightly better" test program comes close to following these rules but still falls short. For example, in order to for each unit test to be truly independent, each should run in a different classloader instance.

Adding a class is also only slightly better. You can now add new unit tests by adding a new method and then adding a corresponding try/catch block to main.

A definite step up, but still short of what you would want in a real unit test suite. The most obvious problem is that large try/catch blocks are known to be maintenance nightmares. You could easily leave a unit test out and never know it wasn't running!

It would be nice if you could just add new test methods and be done with it. But how would the program know which methods to run?

Well, you could have simple registration procedure. A registration method would at least inventory which tests are running.

Another approach would be to use Java's reflection and introspection capabilities. A program could look at itself and decide to run whatever methods are named in a certain way - like those that begin with the letters test, for example.

Making it easy to add tests(the third rul in our earlier list) sounds like another good rule for a unit testing framework.

The support code to realize this rule(via registration or introspection) would not be trivial, but it would be worthwhile. There would be a lot of work up front, but that effort would pay off each time you added a new test.

Happily, the JUnit team has saved you the trouble. The JUnit framework already supports registering or introspecting methods. It also supports using a different classloader instance for each test, and reports all errors on a case-by-case basis.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值