Skip to content

Improve failure message for AreEqual / AreNotEqual #8031

@drieseng

Description

@drieseng

Summary

When the expected value or actual value is null and the other is an empty string, the failure message does not provide much help.

You'll get a test failure stating "Assert.AreEqual failed. Expected:<>. Actual:<> ...."

Background and Motivation

If you have a test failure in Assert.AreEqual(...) where you expect the value to be an empty string, but the actual value is null, the current error message is misleading.

There's a similar issue in the error message that is produced for a failure in AreNotEqual.

To reproduce, run the following unit tests:

using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class Foo
{
    [TestMethod]
    public void AreEqual_ExpectedNullAndActualEmptyString()
    {
        string expected = null;
        string actual = string.Empty;

        Assert.AreEqual(expected, actual);
    }

    [TestMethod]
    public void AreEqual_ExpectedEmptyStringAndActualNull()
    {
        string expected = string.Empty;
        string actual = null;

        Assert.AreEqual(expected, actual);
    }

    [TestMethod]
    public void AreNotEqual_BothEmptyString()
    {
        string expected = string.Empty;
        string actual = string.Empty;

        Assert.AreNotEqual(expected, actual);
    }

    [TestMethod]
    public void AreNotEqual_BothNull()
    {
        string expected = null;
        string actual = null;

        Assert.AreNotEqual(expected, actual);
    }
}

Expected behavior

The following test failures are reported:

  • AreEqual_ExpectedNullAndActualEmptyString:
    Assert.AreEqual failed. Expected:<null>. Actual:<> ...
  • AreEqual_ExpectedEmptyStringAndActualNull:
    Assert.AreEqual failed. Expected:<>. Actual:<null>. ...
  • AreNotEqual_BothEmptyString:
    Assert.AreNotEqual failed. Expected any value except:<>. Actual:<>
  • AreNotEqual_BothNull:
    Assert.AreNotEqual failed. Expected any value except:<null>. Actual:<null>

This is similar to what was done in #7418.
Perhaps it may be even better to also (double)quote string values.

Actual behavior

The following test failures are reported:

  • AreEqual_ExpectedNullAndActualEmptyString:
    Assert.AreEqual failed. Expected:<>. Actual:<> ...
  • AreEqual_ExpectedNullAndActualEmptyString:
    Assert.AreEqual failed. Expected:<>. Actual:<>. ...
  • AreNotEqual_BothEmptyString:
    Assert.AreNotEqual failed. Expected any value except:<>. Actual:<>
  • AreNotEqual_BothNull:
    Assert.AreNotEqual failed. Expected any value except:<>. Actual:<>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions