Skip to content

Add Assert.AreEquivalent to perform deep comparison of objects #4776

Description

Summary

Add an Assert.AreEquivalent method that performs a deep compare on two objects, similar to for example the xUnit Assert.Equivalent method or FluentAssertion's actual.Should().BeEquivalentTo(expected).

Background and Motivation

I'm trying to perform a deep compare of two objects based on the values of the properties, with the option to exclude certain properties.

I was using FluentAssertions for this, but the latest version got a license change where it's now a paid license for commercial use. The only reason I was using this library is because MSTest doesn't provide a deep comparison option. Instead of having to switch to a different library, it would be useful to have this feature in MSTest itself like xUnit. Reducing the number of dependencies and having better feature parity with xUnit.

Proposed Feature

You can pass two objects into Assert.AreEquivalent(expected, actual) and it will perform a comparison of all (public) fields and properties. It will raise an error if the value of a field/property doesn't match. Using a parameter, you can specify that certain properties should be ignored.

It would also be nice if it could perform a deep compare of the items in two collections. E.g. comparing the first item of each collection using the Assert.AreEquivalent method.

Activity

  1. added
    area/assertion
    needs/design
    and removed
    needs/triage
    on Jan 24, 2025
  2. Evangelink commented on Jan 24, 2025

    @Evangelink
    Member

    Hi Ronald Bosma (@ronaldbosma),

    I think that's an interesting idea but we would need to properly design what we want to support or not. FluentAssertion has tons of options so it will require quite some time to match them all (if we want to). We just looked at xUnit and there are some cases that are strange at first look and that we would need to explore better before we start the implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/assertionneeds/design

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions