locked
No Trace or Debug output on ClassInitialize or ClassCleanup methods RRS feed

  • Question

  • My unit test class has the two static methods for ClassInitialize and ClassCleanup marked with their respective attributes. I also have the same for their test equivalent (before a test is run and after a test is run).

    On all of them I am outputting information with either Debug or Trace. I noticed that when I use that on the test method's initialize and cleanup triggers the information is outputed and is visible in the VS. Output window. However, when I do the same in the unit test class's ClassCleanup and ClassInitialize methods, then the Trace or Debug output simply vanishes into the bit bucket and is never seen.

    What am I missing here?


    http://www.virtual-aviation.net/
    Tuesday, January 3, 2012 7:36 PM

All replies

  • Hi,

    Can you sharing a screenshot or your codes here?

    Best Regards,

    Friday, January 6, 2012 4:34 AM
  • Hi DEGT,

    As Lily’s suggestion, would you mind sharing your Visual Studio project or a simple similar Test Project? You can upload it to the sky driver, and then share the download link in your post. Please also attach a screenshot image about the differences in the Output window. We try to debug it with our PC.

    If there's any concern, please feel free to let me know.

    Best Regards,

     


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us
    Monday, January 9, 2012 9:13 AM
  • Hi DEGT,

     

    Sorry for my delay. I create a simple unit test like the following code.

     

    [TestClass]

     

        public class VSTSClass1

        {

            [ClassInitialize]

     

            public static void ClassSetup(TestContext a)

            {

                Debug.Write("Class Setup");

                Trace.WriteLine("Class Setup");

            }

     

            [TestInitialize]

     

            public void TestInit()

            {

                Debug.Write("Test Init");

                Trace.WriteLine("Test Init");

            }

     

            [TestMethod]

     

            public void Test1()

            {

                Debug.Write("Test1");

                Trace.WriteLine("Test1");

            }

     

            [TestCleanup]

            public void TestCleanUp()

            {

                Debug.Write("TestCleanUp");

                Trace.WriteLine("TestCleanUp");

            }

     

            [ClassCleanup]

     

            public static void ClassCleanUp()

            {

                Debug.Write("ClassCleanUp");

                Trace.WriteLine("ClassCleanUp");

            }

    }

    I can get the result like the following screen shot.

     

     

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us
    Friday, January 20, 2012 8:03 AM
  • My  setup is slightly different, I have a base class from which all my tests inherit. It is the base class the one that has the Class Initialize method and attribute. My test classes inherit from this base class and I simply use the test methods in the derived test class. However, I was expecting that the Debug/Trace on the base class method marked with ClassInitialize would show up on the output but they don't.

    It is by no means perfect, I have been trying to recreate the same test "framework" I had been using so far based on NUnit, only now I am using VS test facilities instead.

    Here is the unit tests base class:

    using System;
    using System.Text;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System.Diagnostics;
    
    namespace Tests.ProductCore
    {
        [TestClass]
        public abstract class UnitTestBase<T>
        {
            internal const int OUTPUT_WIDTH = 120;
            private uint testsRun = 0;
            protected TestContext testContextInstance;
            
            /// <summary>
            ///Gets or sets the test context which provides
            ///information about and functionality for the current test run.
            ///</summary>
            public TestContext TestContext
            {
                get { return testContextInstance; }
                set { testContextInstance = value; }
            }
    
            /// <summary>
            /// Runs code before any of the tests defined in the Unit Test class
            /// </summary>
            [ClassInitialize()]
            public static void ClassInitialize(TestContext testContext)
            {
                int repeat = (int)((OUTPUT_WIDTH - testContext.FullyQualifiedTestClassName.Length) / 2);
                System.Diagnostics.Trace.WriteLine(String.Format("{0} {1} {2}", new string('=', repeat), testContext.FullyQualifiedTestClassName, new string('=', repeat)));
                Debug.WriteLine("Class Initialize");
            }
    
            /// <summary>
            /// Runs code after all tests in the class have run
            /// </summary>
            [ClassCleanup()]
            public static void ClassCleanup()
            {
                const string MARKER = "<End Unit Test>";
                int repeat = (int)((OUTPUT_WIDTH - MARKER.Length) / 2);
                System.Diagnostics.Trace.WriteLine(String.Format("{0} {1} {2}", new string('=', repeat), MARKER, new string('=', repeat)));
            }
    
            /// <summary>
            /// Run before each of the test methods
            /// </summary>
            [TestInitialize()]
            public virtual void EachTestInitialize()
            {
                int repeat = (int)((OUTPUT_WIDTH - this.testContextInstance.TestName.Length) / 2);
                System.Diagnostics.Trace.WriteLine(String.Format("{0} {1} {2}", new string('-', repeat), this.testContextInstance.TestName, new string('-', repeat)));
                this.testsRun++;
                PreTestMethod();
            }
    
            public virtual void PreTestMethod()
            {
            }
    
            /// <summary>
            /// Run after each of the test methods
            /// </summary>
            [TestCleanup()]
            public virtual void EachTestCleanup()
            {
                const string MARKER = "<End Test>";
                int repeat = (int)((OUTPUT_WIDTH - MARKER.Length) / 2);
                System.Diagnostics.Trace.WriteLine(String.Format("{0} {1} {2}", new string('-', repeat), MARKER, new string('-', repeat)));
                PostTestMethod();
            }
    
            public virtual void PostTestMethod()
            {
            }
            #region Utilities
            /// <summary>
            /// Reads a text file to a string. It assumes the file is located in the Test_Data directory which can
            /// be configured in the TestDataPath application settings.
            /// </summary>
            /// <param name="filename">filename</param>
            /// <returns></returns>
            public static string LoadTextFile(string filename)
            {
                string data = null;
                if (!String.IsNullOrEmpty(filename))
                {
                    string fullname = System.IO.Path.Combine(Properties.Settings.Default.TestDataPath, filename);
                    using (System.IO.StreamReader sr = new System.IO.StreamReader(fullname))
                    {
                        data = sr.ReadToEnd();
                        sr.Close();
                    }
                }
                return data;
            }
    
            public virtual void Display(T instance, string name)
            {
                Debug.WriteLine(String.Format("Dump [{0}]", name));
                Trace.WriteLine(instance.ToString());
            }
            #endregion
        }
    }
    



    http://www.virtual-aviation.net/
    Monday, January 23, 2012 10:02 PM
  • The trace output from a ClassInitialize and ClassCleanup appears in the result summary.

    You can access it by doing the following

    1. Open the Test Results windw [ Test -> Windows -> Test Results ]
    2. There should be a link named "Test run completed" on the top left corner of the [Test Results] window.
    3. Click the clink
    4. It should open a window with the header "Result Summary" and it will show the debug trace created during ClassInitialize and ClassCleanup
    • Proposed as answer by HJB417 Monday, December 17, 2012 3:24 PM
    Monday, December 17, 2012 3:24 PM