Simulating different times of day when running integration tests, .NET/XUnit

I work on an organization's internal .NET Core 8 application where various dates are stored in different canonical formats, and some comparisons (checking that the current moment falls between a data entity's start and end dates, for example) should be made based on UTC while others should be based on the organization's canonical time zone (US Eastern Time). So it happens that we'll create a test (XUnit) that works fine during working hours, but if someone runs it after hours (for example, within a few hours of either canonical or UTC midnight), it'll fail (which means that the application or the test or both needs tweaking). In addition, we have batch processes that are sensitive to the current day. For example, on a Wednesday it should perform certain operations on data pertaining to dates that precede the previous Wednesday. On other days of the week the process behaves differently. We need to test that this conditional is handled correctly. Ideally, regardless of the day and time we run our tests, we could arrange to have them run multiple times, each time with the tests and the application running under a simulated clock rather than the system clock, so we can time-and-dayproof the code. Without completely overhauling the entire existing application to use some library in place of all the existing invocations of DateTime.Today and DateTimeOffset.Now, is there any way to achieve this?

Apr 15, 2025 - 16:06
 0
Simulating different times of day when running integration tests, .NET/XUnit

I work on an organization's internal .NET Core 8 application where various dates are stored in different canonical formats, and some comparisons (checking that the current moment falls between a data entity's start and end dates, for example) should be made based on UTC while others should be based on the organization's canonical time zone (US Eastern Time). So it happens that we'll create a test (XUnit) that works fine during working hours, but if someone runs it after hours (for example, within a few hours of either canonical or UTC midnight), it'll fail (which means that the application or the test or both needs tweaking).

In addition, we have batch processes that are sensitive to the current day. For example, on a Wednesday it should perform certain operations on data pertaining to dates that precede the previous Wednesday. On other days of the week the process behaves differently. We need to test that this conditional is handled correctly.

Ideally, regardless of the day and time we run our tests, we could arrange to have them run multiple times, each time with the tests and the application running under a simulated clock rather than the system clock, so we can time-and-dayproof the code. Without completely overhauling the entire existing application to use some library in place of all the existing invocations of DateTime.Today and DateTimeOffset.Now, is there any way to achieve this?