문제

I am trying to write a unit test to make sure the results of a method are correct based on different values of a static variable.

Here is a simple example:

public void TestMethod1()
{
     Object1.StaticMember = 1
     Object2 test = new Object2();
     Assert.AreEqual("1", test.getStaticVal());
}

public void TestMethod2()
{
     Object1.StaticMember = 2
     Object2 test = new Object2();
     Assert.AreEqual("2", test.getStaticVal());
}

I was informed that unit tests in VS2012 are excecuted concurrently so there is a posibility of the tests failing. Is this true? How can I write the tests to run one at a time?

도움이 되었습니까?

해결책

There is probably a more elegant way to do it but you can always use a lock object like this...

    private static Object LockObject = new object();

    public void TestMethod1()
    {
        lock(LockObject)
        {
            Object1.StaticMember = 1;
            Object2 test = new Object2();
            Assert.AreEqual("1", test.getStaticVal());
        }
    }

    public void TestMethod2()
    {
        lock (LockObject)
        {
            Object1.StaticMember = 2;
            Object2 test = new Object2();
            Assert.AreEqual("2", test.getStaticVal());
        }
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top