Neil Billingham
Neil Billingham

Reputation: 2285

Displaying the Description attribute of a Unit Test in Visual Studio 2015

Does anyone know how to display the description attribute during a test run in Visual Studio 2015?

For example given this test:

        [TestMethod]
        [TestCategory("With Fakes")]
        [Description("Posting a blog entry without the required data should return a BadRequest")]
        public async Task BlogEntryPostTest1()
        {
          ... do test
        }

How would I get the description to display when running? I don't think Test Explorer will do it; is there any alternative?

Upvotes: 3

Views: 4978

Answers (2)

PANDA MAN
PANDA MAN

Reputation: 202

you can make a utility method that use "System.Reflection" by getting the type of a current instance during runtime, you can pinpoint the method that has description attribute by calling it each time the test initialize

[TestInitialize]
public void TestInit()
{
   DisplayDescription(this.GetType());
}

what actually happens here is

  1. Get the type of the current instance and pass it to DisplayDescription method
  2. Get the test name from test context
  3. Use System.Reflection to pinpoint the test method of the current instance
  4. Get the attribute of the test method
  5. Parse the attribute to DescriptionAttribute and
  6. Display the description text using TestContext writeline

Utility method...

public void DisplayDescription(Type methodType)
{
    string testName = TestContext.TestName;
    MemberInfo method = methodType.GetMethod(testName);
    Attribute attr = method.GetCustomAttribute(typeof(DescriptionAttribute));
    if (attr != null)
    {
        DescriptionAttribute descAttr = (DescriptionAttribute)attr;
        TestContext.WriteLine("Test Description: " + descAttr.Description.ToString());
    }
}

ive been looking for clean solutions for hours and glad i found this tutorial online. i want to share this clever solution so i post it here just in-case someone from the future needs it.

Reference: https://app.pluralsight.com/library/courses/basic-unit-testing-csharp-developers/table-of-contents

Upvotes: 0

David Rogers
David Rogers

Reputation: 2643

I don't think this is possible in test explorer(at least 2015). I also believe that its not supported in ReSharpers "Unit Testing" tool. I think the only way to see the Description Tag displayed next to the unit test is to use NUnit.

You could add your voice to the the proposed change here. Maybe in the next version of test explorer they will add it... For now I would use NUnit(plus GUI) if you really need to see the description, if not maybe you can consider some of these other design proposals.

Upvotes: 1

Related Questions