开发者

Can nunit be configured to drop the app domain per fixture (or per test)

In testing singleton classes we need the single instance to "go away" after each test. Is there a way to configure nunit to recreate the test app domain after each tes开发者_开发百科t, or at least after each fixture?


You could provide the means to renew the singleton instance when testing via a conditional method.

//  CUT
public sealed class Singleton{

    private static Singleton _instance = new Singleton();

    private Singleton()
    {
         //  construct.
    }

    public static Singleton Instance{
        get{
            return _instance;
        }
    }

    [Conditional ("DEBUG")]
    public static void FreshInstance (){
          _instance = new Singleton();
    }
}


//  NUnit
[TestFixture]
public class SingletonTests{

    [SetUp]
    public void SetUp(){
        Singleton.FreshInstance();
    }
}


I needed to do the the exact same thing, so I created a library which basically takes the current test and re-executes it in a new AppDomain. It's a nuget package called NUnit.ApplicationDomain and is open source.

Example Code:

[Test, RunInApplicationDomain]
public void Method()
{
  Console.WriteLine("I'm in a different AppDomain")
}

Related StackOverflow Answer


I guess I'm missing something here Ralph. Just for my own benefit, can you explain why adding methods with the following attributes to your Test Class that drop and recreate your instances wouldn't work for you?

Adding these attributes for methods should make them run before / after each test.

[SetUp]

[TearDown] 

Adding these attributes for methods should make them run before / after the fixture.

[TestFixtureSetUp]

[TestFixtureTearDown]

Is there a reason why using methods with these attributes couldn't create and destroy your domain between tests?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜