locked
TestProperty custom attribute values not appearing in TRX file? RRS feed

  • Question

  • A couple of questions on C# unit tests...

    First, can I add 'TestProperty' custom properties at the unit test class level?

    Second, when I add 'TestProperty' custom properties at the unit test class method level these are not being output in the related TRX file.  Why is this?  Are there any settings I need to tweak to achieve this?  Example code is shown below...

        [TestProperty("use case","find widget by ID"),WorkItem(1964), TestMethod()]
        public void findByIDTestWithInvalidID() {
          WidgetManager target = new WidgetManager(); // TODO: Initialize to an appropriate value
          int anID = 0; // TODO: Initialize to an appropriate value
          string actual;
          actual = target.findByID(anID);
          Assert.IsTrue(new Random().Next(1, 11) <= 8);
        }
    

     

    Tuesday, January 11, 2011 4:37 PM

All replies

  • Hi,

    You cannot use TestProperty attribute on a class, it has to be on a method.

    You will have to use reflection to display the TestProperty in trx.

    [TestMethod]
            [TestProperty("testname", "testvalue")]
            public void TestMethodB()
            {
                object obj = GetType().GetMethod(TestContext.TestName).GetCustomAttributes(typeof(Microsoft.VisualStudio.TestTools.UnitTesting.TestPropertyAttribute), false)[0];
                TestPropertyAttribute attr = (TestPropertyAttribute)obj;
                Console.WriteLine("PropertyName: " + attr.Name + " PropertyValue: " + attr.Value);
               
            }

    Thanks,

    Anuj

    Wednesday, January 12, 2011 12:04 AM
  • thanks...

    Your code suggestion would mean injecting this code (or a subroutine) into every test method - very cumbersome.  Is there not a way of setting this behaviour as a new global custom attribute? In Java I'd be looking at adding an AOP aspect to handle this requirement.

     

    Wednesday, January 12, 2011 10:29 AM
  • Hi,

    You can write this code in test initialize method

    [TestInitialize()]
            public void MyTestInitialize()
            {
                object obj = GetType().GetMethod(TestContext.TestName).GetCustomAttributes(typeof(Microsoft.VisualStudio.TestTools.UnitTesting.TestPropertyAttribute), false)[0];
                TestPropertyAttribute attr = (TestPropertyAttribute)obj;
                Console.WriteLine("PropertyName: " + attr.Name + " PropertyValue: " + attr.Value);
            }

    However, you will still have to add the TestProperty attribute to each test method.

    An alternative way would be to use some global variable instead of using TestProperty attribute and this variable can be assigned a value in TestInitailize or ClassInitailize but I am not sure if that will serve your purpose.

    Thanks,

    Anuj

    Wednesday, January 12, 2011 6:55 PM
  • Hello Davoutuk,

    I am moving your issue to “Visual Studio Team System –Testing” forum to “Visual Studio Unit Test” Forum. As your issue relates to Unit test and you will get better response there.

    Thanks,


    Vicky Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, January 13, 2011 10:49 AM
  • Is there a possibility to get TestProperty attribute information in .trx file without writing this code and only with attributes?
    Wednesday, January 8, 2020 6:59 AM