使用TestDecorator Addin时,Nunit SetUp和TearDown不会运行

时间:2017-03-13 14:15:02

标签: nunit

我正在尝试为Nunit编写一个TestDecorator Addin(2.6.4)。 但启用插件会阻止任何装饰测试的设置和拆卸运行,即使装饰器本身什么都不做(将所有调用推迟到真实测试,如下所示)。

为什么会阻止SetUp / TearDown运行?

装饰:

public class DoNothingTestDecorator : Test
{
    private readonly Test concrete;

    public DoNothingTestDecorator(Test concrete) : base(concrete.TestName)
    {
        this.concrete = concrete;
    }

    public override TestResult Run(EventListener listener, ITestFilter filter)
    {
        return concrete.Run(listener, filter);
    }

    public override string TestType
    {
        get { return concrete.TestType; }
    }

    public override object Fixture
    {
        get { return concrete.Fixture; }
        set { concrete.Fixture = value; }
    }

    public override int CountTestCases(ITestFilter filter)
    {
        return concrete.CountTestCases(filter);
    }

    public override Type FixtureType
    {
        get { return concrete.FixtureType; }
    }

    public override bool Equals(object obj)
    {
        return concrete.Equals(obj);
    }

    public override int GetHashCode()
    {
        return concrete.GetHashCode();
    }

    public override bool IsSuite
    {
        get { return concrete.IsSuite; }
    }

    public override string MethodName
    {
        get { return concrete.MethodName; }
    }

    public override int TestCount
    {
        get { return concrete.TestCount; }
    }

    public override IList Tests
    {
        get { return concrete.Tests; }
    }

    public override string ToString()
    {
        return concrete.ToString();
    }
}

外接:

[NUnitAddin(
    Type=ExtensionType.Core,
    Name = "TestDecoratorAddin",
    Description="Attempts to decorate a test without changing its behavior")
]
public class TestDecoratorAddin : ITestDecorator, IAddin
{
    public bool Install(IExtensionHost host)
    {
        IExtensionPoint testDecorators = host.GetExtensionPoint( "TestDecorators" );
        if (testDecorators == null)
            return false;

        testDecorators.Install(this);
        return true;
    }

    public Test Decorate(Test test, MemberInfo member)
    {
        if (test.Properties.Contains("DecorateMe"))
            return new DoNothingTestDecorator(test);
        return test;
    }
}

测试

[TestFixture]
public class TestToBeDecorated
{
    private bool setupRan = false;

    [SetUp]
    public void SetUp()
    {
        setupRan = true;
    }

    [TearDown]
    public void TearDown()
    {
        setupRan = false;
    }


    [Test]
    [Property("DecorateMe",1)]
    public void TestDecorated() // Fails
    {
        Assert.That(setupRan);
    }

    [Test]
    [Property("Nothing",1)]
    public void TestUnDecorated()  // Passes 
    {
        Assert.That(setupRan);
    }
}

Nunit输出

>nunit-console ExampleTests.dll
NUnit-Console version 2.6.4.14350
Copyright (C) 2002-2012 Charlie Poole.
Copyright (C) 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov.
Copyright (C) 2000-2002 Philip Craig.
All Rights Reserved.

Runtime Environment -
   OS Version: Microsoft Windows NT 6.2.9200.0
  CLR Version: 2.0.50727.8009 ( Net 3.5 )

ProcessModel: Default    DomainUsage: Single
Execution Runtime: net-3.5
.F.
Tests run: 2, Errors: 0, Failures: 1, Inconclusive: 0, Time: 0.0489326845238935 seconds
  Not run: 0, Invalid: 0, Ignored: 0, Skipped: 0

Errors and Failures:
1) Test Failure : ExampleTests.TestToBeDecorated.TestDecorated
     Expected: True
  But was:  False

1 个答案:

答案 0 :(得分:0)

TestMethod.Run(...)依赖于设置Test.Parent属性。属性(错误?)非虚拟,因此不能由装饰者委派。

作为解决方法,可以在装饰的Run方法中设置具体类的Parent属性,如下所示:

public class DoNothingTestDecorator : Test
{
/*...*/
    public override TestResult Run(EventListener listener, ITestFilter filter)
    {
        concrete.Parent=this.Parent;
        return concrete.Run(listener, filter);
    }
/*..*/
}