MicroHelper.Net

雷锋说.对待朋友要MicroHelper,对待敌人要害尔扑
随笔 - 74, 评论 - 431, 引用 - 7

导航

关于

邮件系统不稳定,使用songdming at 263 dot net吧
PageRank

FastCounter by bCentral

 

标签

每月存档

最新留言

  • re: 代码组织
    <a href="http://www.vgoldseller.com/runescape-c-599.html">runescape money</a> ...
    by cxb000(匿名) on 2008/3/26 10:32:11
  • 回复: 看PPMM
    <a target="_new" href="http://www.bestgoldlion.com">http://www.bestgoldlio...
    by runescape gold(匿名) on 2007/12/18 5:13:00
  • 回复: IoC初阶
    <a target="_new" href="http://www.bestgoldlion.com">http://www.bestgoldlio...
    by runescape gold(匿名) on 2007/12/18 5:12:00
  • 回复: 数据分页 (最后更新2003/11/18)
    <a target="_new" href="http://www.sky361.com">http://www.sky361.com</a&...
    by wow power leveling(匿名) on 2007/12/18 5:06:00
  • 回复: iBATIS.Net
    <a target="_new" href="http://www.rsgold-rsgold.com">http://www.rsgold-rsg...
    by runescape money(匿名) on 2007/12/18 5:04:00
  • 回复: 防范Sql注入式攻击
    <a target="_new" href="http://www.rsgold-rsgold.com">http://www.rsgold-rsg...
    by runescape money(匿名) on 2007/12/18 5:03:00
  • 回复: TDD——NUnit的原理
    <a target="_new" href="http://www.rsgold-rsgold.com">http://www.rsgold-rsg...
    by runescape money(匿名) on 2007/12/18 5:03:00
  • 回复: 开源的CMS
    <a target="_new" href="http://www.rsgold-rsgold.com">http://www.rsgold-rsg...
    by runescape money(匿名) on 2007/12/18 5:03:00
  • 回复: CCNet 1.0即将发布
    <a target="_new" href="http://www.sky361.com">http://www.sky361.com</a&...
    by runescape gold(匿名) on 2007/12/18 5:00:00
  • 回复: 关于加班
    为什么这年头加班都成了是我们 的义务了?加班不给加班费也就算了,难得早点回去,还要说三道四的,这社会会为什么会发展成这样啊????
    by 小小(匿名) on 2007/12/14 8:18:00
  • 回复: Presentation草稿:面向对象设计的基本原则
    是的,模式要实事求是,应该是解决问题为主 <br>我搜集了原则方面的文章,有空去看看吧 <br><a target="_new" href="...
    by objecttutor(匿名) on 2007/10/31 17:26:00
  • 回复: 开源的CMS
    支持一下
    by qingxingmeng(匿名) on 2007/10/30 18:03:00
  • 回复: .Net的开源项目
    AnyView(网络警)网络监控软件是一款国内目前最专业的企业级的网络监控软件产品。包含局域网上网监控、邮件监控、聊天监控、BT禁止、流量监视、上下行分离流量限制、并发连接数限制、屏幕监视和录象、硬件...
    by 网络监控软件(匿名) on 2007/10/30 12:33:00
  • 回复: .Net的开源项目
    AnyView(网络警)网络监控软件是一款国内目前最专业的企业级的网络监控软件产品。包含局域网上网监控、邮件监控、聊天监控、BT禁止、流量监视、上下行分离流量限制、并发连接数限制、屏幕监视和录象、硬件...
    by 网络监控软件(匿名) on 2007/10/30 12:32:00
  • 回复: 看PPMM
    好看就成 <br>
    by 11(匿名) on 2007/10/27 15:43:00

广告

 

网络上可以搜索到很多TDD的文章,但是很大一部分只是讲述怎样使用NUnit等工具的使用。只有切身的去体会TDD的每一个环节,才能真正理解TDD。

工具:NUnit的原理

NUnit的原理很简单,就是新建一个TestFixture实例,然后依次调用TestFixture中的Test Case,然后纪录Test Case的运行结果
NUnit的核心对象是TestCase, TestSuit, TestResult

TestCase指一个Test Case,比如一个[Test]属性标记的方法
TestSuit指一组Test Case,比如一个[TestFixture]属性标记的对象
TestResult指Test Case运行的结果,TestResult是一个抽象类,在NUnit中,有两个类是继承自TestResult的:TestCaseResult和
TestSuiteResult

NUnit是怎样运行Test Case的

NUnit定义了一个处理Test Case的抽象类TestCase
TestCase类最重要的方法就是Run()


public override TestResult Run(EventListener listener)
public abstract void Run(TestCaseResult result);


TestCase运行的结果会存入一个TestCaseResult对象

调用Run方法如果传入实现了EventListener接口的对象话,就可以在TestCase实际运行之前以及TestCase运行之后进行自定义的处理
......
listener.TestStarted(this);
Run(testResult);
listener.TestFinished(testResult);
......

NUnit还定义了一个实现抽象类TestCase的通用模版
public abstract class TemplateTestCase : TestCase

TemplateTestCase中Run方法的基本框架为
public override void Run(TestCaseResult testResult )
{
 try
 {
  InvokeSetUp(); //设置环境
  InvokeTestCase(); //运行Test Case
  InvokeTearDown(); //恢复环境
  ProcessNoException(testResult); //无异常退出
 }
 catch
 {
  ProcessException(testResult); //异常处理
 }
}

在Run方法中还会计算Test Case实际运行的时间和所用的内存
DateTime start = DateTime.Now;
long before = System.GC.GetTotalMemory( true );

.... //run test case

long after = System.GC.GetTotalMemory( true );
testResult.Leakage = after - before;
DateTime stop = DateTime.Now;
TimeSpan span = stop.Subtract(start);
testResult.Time = (double)span.Ticks / (double)TimeSpan.TicksPerSecond;

下面几个类都是继承自TestCase类或者TemplateTestCase类
NormalTestCase //一般的Test Case
NotRunnableTestCase //不可运行的Test Case
ExpectedExceptionTestCase //定了期望异常的Test Case


为什么写的Test Case没有自动运行
写Test Case时候要注意,Test Case必须是public的,无参数的,无返回值的函数
参考:
public class NotRunnableTestCase : TestCase
{
 public NotRunnableTestCase(MethodInfo method) : base(method.DeclaringType.FullName, method.Name)
 {
  string reason;

  if (method.IsAbstract)
   reason = "it must not be abstract";
  else if (method.IsStatic)
   reason = "it must be an instance method";
  else if (!method.IsPublic)
   reason = "it must be a public method";
  else if (method.GetParameters().Length != 0)
   reason = "it must not have parameters";
  else if (!method.ReturnType.Equals(typeof(void)))
   reason = "it must return void";
  else
   reason = "reason not known";

  ShouldRun = false;
  IgnoreReason = String.Format("Method {0}'s signature is not correct: {1}.", method.Name, reason);
 }
}

打印 | 张贴于 2004-04-23 15:48:00 | Tag:拥抱变化

留言反馈

#回复: TDD——NUnit的原理 编辑
2007-12-18 05:03:00 | [匿名:runescape money]
#TDD——NUnit的原理 编辑
Ping Back来自:blog.csdn.net
2005-03-22 10:37:00 | [匿名:xiayule]
#回复: TDD——NUnit的原理 编辑
画个类图吧,xUnit的设计还是挺经典的,虽然小
2004-04-23 19:06:00 | [匿名:rIPPER]
#回复: TDD——NUnit的原理 编辑
说实话
在.net中的xp和tdd, 看看Ron Jeffries和James W. Newkirk的文章和书就足以。
2004-04-23 16:51:00 | [匿名:Bz]
对不起,目前本随笔不允许发表新评论.

Powered by: Joycode.MVC引擎 0.5.2.0