Skip to content

Asserts for DateTime do not take into account the Kind #8966

Description

@drieseng

Summary

When asserting DateTime values, the Kind is not taken into account.

Background and Motivation

In our asserts, we want a comparison of two DateTime values to take into account the Kind.
This is current not the case as DateTime ignores any difference on that level in its implementation of Equals (and its operators).

To reproduce, compile and run test following test:

using System;

using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public sealed class DateTimeTest
{
    [TestMethod]
    public void DateTime_AreNotEqual_KindNotEqual()
    {
        var d1 = new DateTime(1973, 08, 13, 00, 00, 00, DateTimeKind.Local);
        var d2 = new DateTime(1973, 08, 13, 00, 00, 00, DateTimeKind.Utc);

        Assert.AreNotEqual(d1, d2);
    }
}

Expected result:

Test should pass.

Actual result:

Assertion failed. Expected values to differ.

notExpected: 13/08/1973 00:00:00
actual:      13/08/1973 00:00:00

Proposal

I don't suppose you'll want to modify the behavior of MSTest as it could break existing tests.
I therefore see two alternate solutions:

  1. We implement extensions methods for DateTime.
  2. MSTest supports specifying custom "comparers" (each for a distinct type).

Do you see other options?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Fields

No fields configured for Feature.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions