UnitTest1.cs 3.07 KB
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TestProject1
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class UnitTest1
    {
        public UnitTest1()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public TestContext TestContext { get; set; }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class


        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        private bool inLoadTest = false;
        private bool dontKnow = true;
        [TestInitialize()]
        public void CheckTestContextType() {
            if (dontKnow)
            {
                // Check if the TestContext type is the one that a loadtest provides
                if (TestContext.GetType().ToString().Equals("Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapterContext"))
                    inLoadTest = true;
                else
                    inLoadTest = false;
                dontKnow = false;
                TestContext.WriteLine("We have a loadtest TestContext: " + inLoadTest);
            }
        }

        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestMethod1()
        {
            // Parameters
            int myThinkTime = 5;

            TestContext.WriteLine("# Current Test Context parameters:");
            IDictionary<string, object> dic = (IDictionary<string, object>)TestContext.Properties;
            foreach (KeyValuePair<string, object> pair in dic)
                TestContext.WriteLine(pair.Key + ": " + pair.Value.ToString());
            TestContext.WriteLine("# End of Current Test Context parameters.");

            // Get think time from load test provided test context if set
            if (TestContext.Properties.Contains("ThinkTime"))
            {
                myThinkTime = int.Parse(TestContext.Properties["ThinkTime"].ToString());
                TestContext.WriteLine("Using thinktime " + myThinkTime + " from test context");
            }

            // Dummy transaction
            TestContext.WriteLine("Starting transaction UnitTestTransaction1");
            if (inLoadTest) TestContext.BeginTimer("UnitTestTransaction1");

            // Think time
            System.Threading.Thread.Sleep(myThinkTime * 1000);

            // End transaction
            TestContext.WriteLine("Ending transaction UnitTestTransaction1");
            if (inLoadTest) TestContext.EndTimer("UnitTestTransaction1");

        }
    }
}