Skip to content

Improve failure message for Assert.IsNull #8032

@drieseng

Description

@drieseng

Summary

When a test failure is reported by Assert.IsNull(...), no information is provided on the actual value.

Background and Motivation

When you assert that a given "value" (object reference) is null, the failure that is reported when it isn't null could provide more information on the "value".

This is definitely useful if the type of value implements IFormattable or is a nullable value type for which the underlying type implements IFormattable.

To reproduce, run the following test class:

using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class Foo
{
    [TestMethod]
    public void IsNull()
    {
        string actual = "XX";

        Assert.IsNull(actual);
    }
}

Expected behavior

The following test failure is reported:
Assert.IsNull failed. Expected:<null>. Actual:<XX>. 'value' expression: 'actual'.

Actual behavior

The following test failure is reported:
Assert.IsNull failed. 'value' expression: 'actual'.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions