Skip to content

Provide an Assert overload to explicitly check self equality #9972

Description

@RobSwDev

Some of our types override Equality. One of the basic things to check is that an instance is equal to itself.
However, this triggers an MSTEST0032 error.
Perhaps an Assert.SelfEqual(myInstance) could help distinguish between the majority of cases where self-comparison is a typo, and the few cases where it is a deliberate, legitimate test?

Activity

  1. added theissue type on Jul 15, 2026
  2. added
    needs/triage
    on Jul 15, 2026
  3. Youssef1313 commented on Jul 15, 2026

    @Youssef1313
    Member

    I would consider MSTEST0032 a false positive if flagged assertion is going to go through user code, because that means we cannot know if it's always true/false.

  4. RobSwDev commented on Jul 15, 2026

    @RobSwDev
    Author

    Youssef Fahmy (@Youssef1313) Technically I agree. But i found a dozen self-comparisons in our codebase, and only 2 were "intentional". Many of the accidental ones probably went through a custom (user code) equality method. They were still bugs in the test code.

  5. Evangelink commented on Jul 15, 2026

    @Evangelink
    Member

    RobSwDev just to be sure, are you saying that if the analyzer wasn't reporting when the type use in Assert.AreEqual/AreNotEqual has cutom equality operator you would have missed broken code/tests?

  6. RobSwDev commented on Jul 15, 2026

    @RobSwDev
    Author

    Perhaps not, actually. I was confusing this with the "Collections implementing IEquatable" issue. So Youssef1313 was right.

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

    No labels
    No labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions