Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

.net

nunit

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 test, or at least after each fixture?

like image 525
Ralph Shillington Avatar asked Feb 25 '10 15:02

Ralph Shillington


1 Answers

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();
    }
}
like image 65
Grokodile Avatar answered Sep 30 '22 04:09

Grokodile