【发布时间】:2013-07-13 05:37:26
【问题描述】:
我开始按照 Phil Haack 建议的here 的方式组织我的单元测试。我的测试类每个被测公共方法都有一个嵌套类。嵌套类派生自外部类,以便继承其设置代码。
我现在有一个案例,我想测试一个抽象基类和两个派生类的简单层次结构。我想显式地测试每个派生类与基类的交互,而不是使用某种模拟派生类测试基类。
我过去曾使用一个基础测试夹具和每个派生类一个派生夹具来完成此操作,其中派生夹具必须为基础夹具中的测试实现一些模板方法:
[TestFixture]
public abstract class BaseFixture
{
protected abstract MyBaseClassUnderTest GetTestInstance();
[Test]
public void SomeMethod_SomeCondition_SomeOutcome()
{
var sut = GetTestInstance();
//test base class behavior here
}
//More base class tests here
}
public class DerivedFixture : BaseFixture
{
protected override MyBaseClassUnderTest GetTestInstance()
{
return new DerivedInstance();
}
//Tests for derived class go here
}
有没有人有想法,我该如何解决这个问题?现在我看不到如何结合这两种方法,因为继承策略不同(从外部类继承设置代码与从基类夹具继承基类测试)。
【问题讨论】:
标签: c# .net unit-testing nunit