Follow @RoyOsherove on Twitter

Implementing Unit Test MetaData using XtUnit custom attributes

Regarding my last post on xml comments used inside a unit test Here's how you can implement the same "metadata on a test" equivalent using XtUnit custom attributes (note that you need to inhering from a custom class for this):


using NUnit.Framework;
using XtUnit.Framework.Internal;

    public class CustomDataAttributes:TestFixtureBase
        [Data("sql","Select * from categories")]
        public void useCustomDataAttributes()
            string actual = DataAttribute.items["sql"];
            string expected = "Select * from categories";

    class DataAttribute:ProcessingAttributeBase
        private readonly string data = string.Empty;
        private readonly string name = string.Empty;
        public string Name {get { return name; }}
        public string Data { get { return data; } } 
        public DataAttribute(string name, string data)
   = data;
   = name;

        public static readonly Dictionary<string, string> items =                                         new Dictionary<string, string>();         public static Dictionary<string, string> Items
        {get { return items; }  }

        protected override void OnPreProcess()
            items[name] = data;

        protected override void OnPostProcess()
        {   }

Ubuntu's #1 bug report

Unit Test Tip: Encode test data as part of your test xml comments