开发者

Maintaining a singleton object in WatiN tests

I have a problem with keeping an object singleton in NUnit tests.

This is my base class:

[TestFixture]
public class SuiteBase
{
    public MyLib lib = null;

    [TestFixtureSetUp]
    public void TestFixtureSetUp()
    {
        lib = MyLib.Instance;
    }

    [TestFixtureTearDown]
    public void TestFixtureTearDown()
    {
    }
}

And this is one of my test suites.

[TestFixture]
public class Suite1 : SuiteBase
{
    [Test]
    public void Test1()
    {
        lib.Foo();
        //...
    }

    [Test]
    public void Test2()
    {
        lib.Bar();
        //...
    }
}

And MyLib class is defined as the following:

//singleton class
public sealed class MyLib
{
    public IE browser;
    //other public fields...

    public static MyLib Instance
    {
        get
        {
            if (instance == null)
            {
      开发者_运维知识库          instance = new MyLibLib();

                using (StreamWriter sw = new StreamWriter(@"c:\test.txt", true))
                {
                    sw.WriteLine("Creating object");
                    sw.Flush();
                }
            }

            return instance;
        }
    }

    private static MyLib instance;

    private MyLib()
    {
        browser = new IE();
        //init the rest of public fields
    }
}

The problem is the singleton class object is being created with every test, I run using:

int result = NUnit.ConsoleRunner.Runner.Main(new string[] { 
"/run:" + MyAssembly.GetName().Name + testSuiteName + "." + testCaseName, MyAssembly.Location, 
                            "/process:Single",
                            "/domain:Single",
                            "/nothread",
                            "/timeout:" + testTimeout
        }
    );

Any idea would be appreciated. Thanks.


After changing the "/domain:Single" to "/domain:None", its creating a single object now

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜