5

以前没有编写过单元测试框架,在我看来,如果程序集中的某些类型都需要针对类似的事情进行测试,那么可继承的 Fact 属性将使编写抽象测试类或测试接口变得更容易。

是否存在 Fact 不可继承的设计原因?我认为其他使用属性来识别测试方法的测试框架(NUnit、MSTest、MbUnit 等)的设计类似。我错过了什么?

这是 xunit(版本 1.9.1.1600)的 FactAttribute 的开始:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class FactAttribute : Attribute
{

我试图理解为什么它看起来不像下面这样:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class FactAttribute : Attribute
{ 
4

3 回答 3

2

FactAttribute 是可继承的。

看看这篇文章:http: //iridescence.no/post/Extending-xUnit-with-a-Custom-ObservationAttribute-for-BDD-Style-Testing.aspx

我觉得有时候真的很有用。

于 2012-07-31T12:39:44.093 回答
2

我经常在我的测试中使用模板模式(这似乎是你想要完成的),它与 xUnit.net 一起工作得很好。

public abstract class TestBase
{
    [Fact]
    public void Verify()
    {
        // Step 1
        // Step 2

        // Overridable step 3
        DoStuff();

        // Assert
    }

    protected abstract void DoStuff();
}

public class Test1 : TestBase
{
    protected override void DoStuff()
    {
        // Test1 variation
    }
}

public class Test2 : TestBase
{
    protected override void DoStuff()
    {
        // Test2 variation
    }
}

验证执行了两次——一次在实例 Test1 上,另一次在 Test2 实例上。

希望这可以帮助。

于 2012-09-21T22:35:54.807 回答
1

我从来没有听说过属性是可继承的,就像类一样。尽管使用通用测试方法编写基类并将 [Fact] 属性应用于派生类中的方法,但您应该没有问题。

于 2012-07-18T16:23:41.463 回答