Click here to Skip to main content
15,881,681 members
Articles / Programming Languages / C#

Testing Equality of Two Objects

Rate me:
Please Sign up or sign in to vote.
3.00/5 (9 votes)
13 Jan 2008CPOL2 min read 45.4K   409   7   10
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:

  1. I named it AssertPublicPropertiesEqual, but feel free to change that name if you don't like it.
  2. 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.
  3. It attempts to detect and short-circuit circular references.
  4. It allows you to supply an ‘ignore list’ of properties. For example, you might consider two List<string> objects that contain the same strings to be equal even though their Capacity property values are different.
  5. It ignores indexed properties (such as String.Chars). Normally these will be covered by the check of some other non-indexed property.
  6. The original contains this test: pi.PropertyType.IsAssignableFrom(typeof(IEnumerable)). That seemed to be the wrong way around, so I changed it to typeof(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:

C#
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:

C#
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 PropertyComparionExclusions, 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:

  1. Is this useful for you?
  2. 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United Kingdom United Kingdom
Carl Johansen is a .NET developer blah blah, lives in London blah blah, hobbies include juggling blah.

Comments and Discussions

 
QuestionObject inside an object Pin
kani1219-Jan-16 22:13
kani1219-Jan-16 22:13 
Can this solution work for comparing an object containing another object inside it or a list of objects inside it
AnswerRe: Object inside an object Pin
Franz Justin Buenaventura15-Apr-17 10:56
Franz Justin Buenaventura15-Apr-17 10:56 
GeneralMy vote of 4 Pin
stevenkjamesUSFS14-Apr-14 5:36
stevenkjamesUSFS14-Apr-14 5:36 
Question[My vote of 1] Didnt work for me - Stack overflow error. Pin
shyam_11-Mar-09 16:37
shyam_11-Mar-09 16:37 
AnswerRe: [My vote of 1] Didnt work for me - Stack overflow error. Pin
shyam_11-Mar-09 16:52
shyam_11-Mar-09 16:52 
GeneralRe: [My vote of 1] Didnt work for me - Stack overflow error. Pin
Carl Johansen12-Mar-09 0:43
Carl Johansen12-Mar-09 0:43 
GeneralRe: [My vote of 1] Didnt work for me - Stack overflow error. Pin
shyam_12-Mar-09 18:04
shyam_12-Mar-09 18:04 
AnswerRe: [My vote of 1] Didnt work for me - Stack overflow error. Pin
Carl Johansen23-Mar-09 13:24
Carl Johansen23-Mar-09 13:24 
AnswerRe: [My vote of 1] Didnt work for me - Stack overflow error. Pin
Carl Johansen12-Mar-09 2:34
Carl Johansen12-Mar-09 2:34 
GeneralJust what I was looking for Pin
DancingJester15-Aug-08 4:55
DancingJester15-Aug-08 4:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.