In our codebase there is an XUnit theory that uses the attribute [Theory, MemberData(nameof(CancelledDates))]
. Here is the method that provides the MemberData.
public static IEnumerable<object[]> CancelledDates()
{
return new[]
{
new object[] { DateTime.Today.AddDays(5), DateTime.Today.AddDays(10), false, "Incident date cannot be before the Cover Start date of the Policy." },
new object[] { DateTime.Today.AddDays(-10), DateTime.Today.AddDays(-5), false, "Incident date cannot be after the Policy Cover End date." },
new object[] { DateTime.Today.AddDays(-5), DateTime.Today, true, string.Empty },
new object[] { DateTime.Today, DateTime.Today.AddDays(5), true, string.Empty },
};
}
The problem is the third entry; when I ran tests around 16:00 on 2023/07/13 the test failed because the dates were one day earlier than they should have been. Rather than 2023-07-08T00:00:00.0000000+02:00
and 2023-07-13T00:00:00.0000000+02:00
as expected, the dates provided to the test were 2023-07-07T00:00:00.0000000+02:00
and 2023-07-12T00:00:00.0000000+02:00
. This results in the test failing as the second date is expected not to be earlier than today in the code under test.
The strange thing is that this does not happen every time the test is run. It seems to happen only when running tests in Visual Studio and Azure DevOps builds that require tests to pass never seem to fail on this test. The date and time on my machine are correct and the time zone is set to UTC+2.
This has been happening to me randomly for several months; I'm tired of it and would love to find a solution or at least understand why it happens. Any ideas?