Tuesday, 12 February 2013

Unit tests - some tips

1. TestContext.WriteLine - If we want to add additional information in to our test result, we use this method within our Test method. To view this additional information, double the test in Test Result window

2. One thread per test - MSTest creates one thread per test. But tests are run sequentially without any order. Try below to verify

        [TestMethod]
        public void TestMethod1()
        {
            System.Threading.Thread.Sleep(1000);
            TestContext.WriteLine("{0}", System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
        }
 
        [TestMethod]
        public void TestMethod2()
        {
            System.Threading.Thread.Sleep(1000);
            TestContext.WriteLine("{0}", System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
        }
 
        [TestMethod]
        public void TestMethod3()
        {
            System.Threading.Thread.Sleep(1000);
            TestContext.WriteLine("{0}", System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
        }
 
3. If we have multiple cores, we can run tests parallel using this

No comments: