Tuesday, January 12, 2010

Testing C# Class Properties With NUnit - Part 2

In a previous post I showed how to auto-test object properties in a NUnit test. I forgot to include a function called GetProperties() which was referenced in the code example. Here it is:


       /// <summary>
        /// An test that iterates through an object's properties and does
        /// a very minimal test on get/set integrity.
        /// </summary>
        /// <param name="patient"></param>
        /// <returns></returns>
        public ArrayList GetProperties(Patient patient)
        {
            ArrayList Properties = new ArrayList();
            PropertyInfo[] propertyInfo = patient.GetType().GetProperties();

            foreach(PropertyInfo item in propertyInfo)
            {
                Properties.Add(item.Name);
            }

            return Properties;
        }

Of course this is pretty simple minded and should be re-factored in any number of ways. Most importantly to get rid of the specific object reference and make it generic. Cut and paste programming makes us angry, very very angry.