Unit testing extension for equality testing

Posted on


I have created an extension for unit-testing my own equality implementations.
Is it a good approach in general to have such extensions for unit testing?
What unit-testing extensions do you use?

public static class EqualityExtensions
        /// <summary>
        /// Checks that all variations Equals method conforms to equality by properties
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="variations"></param>
        /// <param name="propertyAccessors">Property accessors specifying which properties contribute to equality</param>
        /// <returns>True, if all instances from a set conform to property equality</returns>
        /// <example>
        /// Assert.IsTrue(from name in new[] {"foo","bar"} from count in new[] {1,2} select new MyClass{Name=name,Count=count}).ConformToPropertyEquality(x=>x.Name,x=>x.Count))
        /// </example>
        public static bool ConformToPropertyEquality<T>(this IEnumerable<T> variations, params Func<T, object>[] propertyAccessors)
            var propertyComparisions =
                from lhs in variations
                from rhs in variations
                        (from comparator in propertyAccessors select Equals(comparator(lhs), comparator(rhs))).All(c => c),
                        Equals(lhs, rhs)

            return propertyComparisions.All(p => p);


It’s perfectly OK. As an example SharpTestEx is a whole library with such an extensions.

Leave a Reply

Your email address will not be published. Required fields are marked *