Question

Ian Ceicys on Thu, 13 Aug 2015 13:50:57


I have the following unit test and I am trying to write out the Description to Output but I can't seem to figure out how to get the Description property. 

Here is my unit test (UnitTest1.cs)

using System;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1:BaseClass
    {

        [TestCategory("Example"), TestCategory("Development"),
         TestMethod]
        [Microsoft.VisualStudio.TestTools.UnitTesting.Description("Example Unit Testing Method")]
        public void UnitTest_Should_be_Able_to_Query_Name_Categories_and_Description()
        {
            base.TestInitialize(MethodBase.GetCurrentMethod());
            Console.WriteLine("Hello World Unit Test");
        }
        }
    }

Here is my BaseClass.cs

using System;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject1
{
    public class BaseClass
    {
        public BaseClass()
        {
        }
        public void TestInitialize(MethodBase method)
        {
            //Get the attribute collection for method MainTest
            MethodInfo t = (typeof(UnitTest1).GetMethod(method.Name.ToString()));
            object[] o = t.GetCustomAttributes(false);
            TestCategoryAttribute attr = null;

            //Scan through all attribute applied to the method 
            //to verify whether there are TestCategoryAttribute apply to it
            Console.WriteLine("Test Name = " + method.Name);
            Console.WriteLine("Test Description = ?????");
            foreach (object m in o)
            {
                attr = m as TestCategoryAttribute;

                if (attr != null)
                {
                    //Type out the value of TestCategory
                    foreach (string mm in attr.TestCategories)
                    {
                        Console.WriteLine("Test Category = " + mm);
                    }

                }
            }
        }
    }
}

How can I write out the Test Method Description to the output log?



Ian Ceicys



Sponsored



Replies

Ian Ceicys on Thu, 13 Aug 2015 13:58:01


I figured out the solution. I need to use the DescriptionAttribute from the docs.

I updated the code here to output the description. 

using System;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject1
{
    public class BaseClass
    {
        public BaseClass()
        {
        }
        public void TestInitialize(MethodBase method)
        {
            //Get the attribute collection for method MainTest
            MethodInfo t = (typeof(UnitTest1).GetMethod(method.Name.ToString()));
            object[] o = t.GetCustomAttributes(false);
            TestCategoryAttribute attr = null;
            DescriptionAttribute desc = null;

            //Scan through all attribute applied to the method 
            //to verify whether there are TestCategoryAttribute apply to it
            Console.WriteLine("Test Name = " + method.Name);
            foreach (object m in o)
            {
                attr = m as TestCategoryAttribute;

                if (attr != null)
                {
                    //Type out the value of TestCategory
                    foreach (string mm in attr.TestCategories)
                    {
                        Console.WriteLine("Test Category = " + mm);
                    }

                }
            }

            foreach (object n in o)
            {
                desc = n as DescriptionAttribute;

                if (desc != null)
                {
                        Console.WriteLine("Description = " + desc.Description);
                }
            }
        }
    }
}

Hope this helps someone!


Ian Ceicys


Tina-Shi on Mon, 17 Aug 2015 02:31:51


Hi lan,

I am glad that you have solved this issue and thank you share the solution here so that it would be helpful for other members encountered similar issue.

Best Regards,

OdsalBantam on Wed, 20 Sep 2017 13:08:12


Thanks for identifying the root of the solution.  I've often thought that more should be made of the DescriptionAttribute value - self-describing unit test names can only go so far before they can become unwieldy.  Sometimes you also need a fuller description of what the test is trying to highlight. 

Since the test descriptions vanished from the standard MS Test Explorer window after VS2010, I've been looking for a way to reinstate them on screen.  A combination of the following steps will help:

  1. Add a TestContext property to the test class. 
  2. Use TestContext.WriteLine() to dump any useful output to the Output window (follow the Output link which appears when you click on a completed test in the MS Test Explorer window).
  3. Include a call to MethodBase.GetCurrentMethod() within any unit test which requires a test description.
  4. Include the following code (based on the ideas in the original post) in a helper methods file, which should ideally be visible across multiple test projects:

        /// <summary>
        /// Gets the <see cref="DescriptionAttribute"/> value for a test, where specified
        /// </summary>
        /// <param name="testMethodInfo">The current method (of the executing test)</param>
        /// <returns>The test description, if specified</returns>
        public static string GetTestDescription(MethodBase testMethodInfo)
        {
            var t = (DescriptionAttribute)testMethodInfo.GetCustomAttribute(typeof(DescriptionAttribute));

            return t == null ? string.Empty : t.Description;
        }

A call to this function within TestContext.WriteLine() will dump the test description into the Output window. 

This is especially helpful if there are a large number of tests across multiple test projects, with either identical or similar test names.