单元测试 – 单元测试模板方法设计模式
|
假设以下模板方法实现: public abstract class Abs
{
void DoYourThing()
{
log.Info("Starting");
try
{
DoYourThingInternal();
log.Info("All done");
}
catch (MyException ex)
{
log.Error("Something went wrong here!");
throw;
}
}
protected abstract void DoYourThingInternal();
}
现在,有很多关于如何测试Abs类的信息,并确保调用DoYourThingInternal. public class Conc : Abs
{
protected override void DoYourThingInternal()
{
// Lots of awesome stuff here!
}
}
我不想做conc.DoYourThing(),因为这将调用已经单独测试的父方法. 我想只测试重写方法. 有任何想法吗? 我不认为DoYourThingInternal()与DoYourThing()分开(就像在两个单独的代码模块中可以单独测试一样),因为无论如何你都无法单独实例化你的抽象类,而且2个方法将会总是一起跑.此外,DoYourThingInternal()可以访问您班级的所有受保护成员,并可以修改它们,对DoYourThing()有潜在的副作用.所以我认为测试DoYourThing()与DoYourThingInternal()的具体实现完全隔离是危险的.但是,这并不意味着您不能对DoYourThing()的预期行为进行单独测试,这些测试必须在Abs的所有实现和DoYourThingInternal()的预期行为中保持相同. 您可以使用(抽象)基本测试类来定义对DoYourThing()期望的一般行为的测试.然后创建与Abs的实现一样多的测试子类,并对每个实现的细节进行单元测试. 来自基础测试类的测试将被继承,当您运行任何子类的测试时,DoYourThing()的继承测试也将运行: public abstract class AbsBaseTest
{
public abstract Abs GetAbs();
[Test]
public void TestSharedBehavior()
{
getAbs().DoYourThing();
// Test shared behavior here...
}
}
[TestFixture]
public class AbsImplTest : AbsBaseTest
{
public override Abs GetAbs()
{
return new AbsImpl();
}
[Test]
public void TestParticularBehavior()
{
getAbs().DoYourThing();
// Test specific behavior here
}
}
见http://hotgazpacho.org/2010/09/testing-pattern-factory-method-on-base-test-class/ 不知道所有单元测试框架是否支持抽象测试类继承(我认为NUnit确实如此). (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
