Testing Equality of Two Objects





3.00/5 (9 votes)
A general purpose C# method for testing whether two objects have the same values in their respective public properties
Introduction
A while ago, I was looking for a way to compare two objects as part of a unit test. I didn't want Assert.AreSame()
because I didn't just want to see if they were the same object, and I didn't want Assert.AreEqual()
because I didn't want to have to override Equals()
. Ideally I wanted a general utility that would compare any two objects and say whether they were equal. I decided to define “equal” as “having the same value for all public
properties”. I wanted a deep comparison, so if a public
property is another object then all of its public
properties should also be checked.
My search led me to the AssertGraphPropertiesEqual function written by Keith Brown. This was just the kind of thing I had in mind, but in using it I made a number of changes that I think made it generally more useful. I would like to share my version with you. These are the main changes in my version:
- I named it
AssertPublicPropertiesEqual
, but feel free to change that name if you don't like it. - It is more consistent in its handling of an object that is passed as a parameter to the method and an object that is a property of that parameter. Making this change solved a number of problems that I was having with the original.
- It attempts to detect and short-circuit circular references.
- It allows you to supply an ‘ignore list’ of properties. For example, you might consider two
List<string>
objects that contain the samestring
s to be equal even though theirCapacity
property values are different. - It ignores indexed properties (such as
String.Chars
). Normally these will be covered by the check of some other non-indexed property. - The original contains this test:
pi.PropertyType.IsAssignableFrom(typeof(IEnumerable))
. That seemed to be the wrong way around, so I changed it totypeof(IEnumerable).IsAssignableFrom(objectType)
.
Using the Code
The method is intended to be used in unit testing, so it uses standard Asserts to perform the comparisons. I have tried it with both Visual Studio Team System (VSTS) and NUnit. Just include the appropriate using
statement for the library that you use.
A typical call would be something like this:
MyClass expectedObject = the expected result of the test;
MyClass actualObject = MethodBeingTestedThatReturnsAMyClassObject();
UnitTestingHelper.AssertPublicPropertiesEqual(expectedObject, actualObject);
As I mentioned above, you can specify properties to be ignored during the comparison. Here's how to ignore the Capacity
property of List<string>
objects:
MyClass expectedObject = the expected result of the test;
MyClass actualObject = MethodBeingTestedThatReturnsAMyClassObject();
IgnoreProperties ignoreProps = new IgnoreProperties();
ignoreProps.Add(new PropertyComparisonExclusion(typeof(List<string>), "Capacity"));
UnitTestingHelper.AssertPublicPropertiesEqual
(expectedObject, actualObject, ignoreProps);
When defining PropertyComparionExclusion
s, you can use the typeAction
property to specify whether to ignore the property only on the specified type, or on the type and all its derived types. The default is PropertyComparisonExclusionTypeAction.MatchExactType
, meaning that the property is ignored only on the specified type.
Conclusion
I have two questions:
- Is this useful for you?
- Have I got it right?
I've thrown various objects at it, but the possible inputs are literally infinite so please let me know if you find a problem with it.