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;

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

    [AttributeUsage(AttributeTargets.Method)]
    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)
        {
            this.data = data;
            this.name = 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()
        {   }
    }
Published Monday, June 25, 2007 8:10 PM by RoyOsherove

Comments

Thursday, June 28, 2007 6:28 PM by Vadim

# re: Implementing Unit Test MetaData using XtUnit custom attributes

Tuesday, September 04, 2007 4:07 PM by Geovanny Tejeda

# re: Implementing Unit Test MetaData using XtUnit custom attributes

Does anybody knows if this links is still valid?

www.teamagile.com/.../TeamAgile.XtUnit.Bin.zip

When i download this and add to my project it allows me to add the [Test, DataRollBack]

(which is on namespace: TeamAgile.ApplicationBlocks.Interception.UnitTestExtensions)

but it doesn't give me any errors or anything, it does however adds it to the Database anyway... any clues? or alternatives?

thanks

Tuesday, September 04, 2007 11:03 PM by RoyOsherove

# re: Implementing Unit Test MetaData using XtUnit custom attributes

Geovanny : make sure your class inherits from TestFixtureBase or InterceptableObject (part of xxtunit API) or this will not work.

Roy.