享受代码,享受人生

SOA is an integration solution. SOA is message oriented first.
The Key character of SOA is loosely coupled. SOA is enriched
by creating composite apps.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

AssertThat --- 测试的重用

Posted on 2005-05-22 21:35  idior  阅读(3563)  评论(2编辑  收藏  举报

前段时间写了一篇随笔介绍了一下AssertThat的用法.
今天突发奇想由此联想到了测试代码的复用. (哈,我也会突发奇想了)

其实在编写测试案例的过程中有很多的测试方法仅仅是因为换了一个对象我们就要重写一次,当然你可以使用Ctrl+C,Ctrl+V.不过这显然是一个Bad Smell. 有没有什么改进的方法呢?

看看AssertThat的结构.

Assert.AssertThat(3, Between(24));
第一个参数是待测试的对象, 第二个参数是将要测试的约束.

你有没有想到什么?这难道不是一个很好的复用结构吗?

如果把约束做成Composition模式,就可以将以往一个的测试方法做成一个约束,复用就更方便了.
我们只需要事先写好一个约束以后就可以在多个类似的对象上使用.

Do u think it's a good idea? Maybe i can email to NUnit team?