TimeAssertions.TUnit
0.9.0
Prefix Reserved
dotnet add package TimeAssertions.TUnit --version 0.9.0
NuGet\Install-Package TimeAssertions.TUnit -Version 0.9.0
<PackageReference Include="TimeAssertions.TUnit" Version="0.9.0" />
<PackageVersion Include="TimeAssertions.TUnit" Version="0.9.0" />
<PackageReference Include="TimeAssertions.TUnit" />
paket add TimeAssertions.TUnit --version 0.9.0
#r "nuget: TimeAssertions.TUnit, 0.9.0"
#:package TimeAssertions.TUnit@0.9.0
#addin nuget:?package=TimeAssertions.TUnit&version=0.9.0
#tool nuget:?package=TimeAssertions.TUnit&version=0.9.0
TimeAssertions.TUnit
Scope: Test projects only. Not intended for production code.
TUnit-native fluent time-assertion DSL on top of Microsoft.Extensions.Time.Testing.FakeTimeProvider. Adds FakeTimeProvider state assertions, TimeProvider-aware DateTimeOffset recency / past / future checks, plus the cross-cutting .WithinTimeBudget(TimeSpan) chain extension. AOT-compatible, trimmable, no reflection.
Full documentation, "Why TimeProvider in tests", cookbook, design notes, and roadmap: github.com/JohnVerheij/TimeAssertions.TUnit
Install
dotnet add package TimeAssertions.TUnit
TimeAssertions (the framework-agnostic core) and Microsoft.Extensions.TimeProvider.Testing come transitively. Requirements: TUnit 1.53.0 or later, .NET 10.
The source-generated entry points (HasAdvancedExactly, HasAdvancedApproximately, HasUtcNow, HasUtcNowApproximately, IsRecent, IsBeforeNow, IsAfterNow, WithinTimeBudget, WithinTimeBudgetCapturing, WasInvokedAtMostOncePer, HasNoActiveTimers, HasActiveTimerCount, HasActiveTimers, HasAtLeastActiveTimerCount, HasNoActiveTimersEventually, HasActiveTimerCountEventually, HasAtLeastActiveTimerCountEventually, HasAtMostActiveTimerCountEventually, HasActiveTimersEventually, HasNextTimerDueApproximately, HasPendingTimerDueWithin, HasTimerFiredCount, HasNoTimerFired, HasTimerFiredAtLeast) auto-import via TUnit.Assertions.Extensions. Add the following to a GlobalUsings.cs in your test project for the call-site and FakeTimeProvider namespaces:
global using Microsoft.Extensions.Time.Testing;
global using TimeAssertions;
global using TimeAssertions.TUnit;
Quick start
[Test]
public async Task PreReleaseExpiration_advances_state_after_clock_moves_forward()
{
var fakeTime = new FakeTimeProvider();
var service = new ExpirationService(fakeTime);
fakeTime.Advance(TimeSpan.FromMinutes(31));
await Assert.That(fakeTime).HasAdvancedExactly(TimeSpan.FromMinutes(31));
await Assert.That(service.LastRefresh).IsRecent(TimeSpan.FromSeconds(1), fakeTime);
// Cross-cutting timing budget on any behavioral assertion chain
await Assert.That(service.IsExpiredAsync())
.IsTrue()
.And.WithinTimeBudget(TimeSpan.FromMilliseconds(500));
}
Entry points
| Method | Purpose |
|---|---|
HasAdvancedExactly(TimeSpan) / HasAdvancedApproximately(total, tolerance) |
FakeTimeProvider advanced by exact / approximate amount (renamed from HasAdvanced / HasAdvancedBy in v0.2.0; old names [Obsolete] until v0.4.0) |
HasUtcNow(DateTimeOffset) / HasUtcNowApproximately(expected, tolerance) |
FakeTimeProvider is at exact / approximate moment |
IsRecent(TimeSpan, TimeProvider?) |
DateTimeOffset is within window before "now" of supplied (or system) clock |
IsBeforeNow(TimeProvider) / IsAfterNow(TimeProvider) |
DateTimeOffset ordering relative to supplied clock |
WithinTimeBudget(TimeSpan) |
Cross-cutting timing budget; chains via .And after any behavioral assertion |
WithinTimeBudgetCapturing(TimeSpan, Action<TimeSpan>) |
Same as WithinTimeBudget plus a callback that receives the measured elapsed on every evaluation path except external cancellation (added in v0.2.0; cancellation-skip behavior added in v0.5.0) |
WasInvokedAtMostOncePer(this IReadOnlyList<DateTimeOffset>, TimeSpan interval) |
Rate-limit assertion on a recorded invocation log: every consecutive gap is at least interval (added in v0.5.0) |
HasNoActiveTimers() / HasActiveTimerCount(int) on ObservableTimeProvider |
Timer-leak assertions: no undisposed timers / exact active-timer count, naming each survivor by its schedule on failure (added in v0.6.0) |
HasActiveTimers() / HasAtLeastActiveTimerCount(int) on ObservableTimeProvider |
Positive-count assertions: at least one / at least count active timers, for a lower bound rather than an exact count (added in v0.7.0) |
HasNoActiveTimersEventually(TimeSpan, ...) / HasActiveTimerCountEventually(int, TimeSpan, ...) on ObservableTimeProvider |
Real-time poll until the active count reaches zero / a target count, for the asynchronous disposal race a synchronous check cannot see (added in v0.7.0) |
HasNextTimerDueApproximately(TimeSpan, TimeSpan) / HasPendingTimerDueWithin(TimeSpan, TimeSpan) on ObservableTimeProvider |
Pending-timer due-time assertions: inspect the next scheduled timer's due time without advancing the clock, within a tolerance or an inclusive range (added in v0.6.0) |
HasTimerFiredCount(int) / HasNoTimerFired() / HasTimerFiredAtLeast(int) on ObservableTimeProvider |
Timer-fire assertions: how many times timer callbacks ran in total, cumulative and surviving disposal (added in v0.9.0) |
Failure diagnostics
On a failed assertion, the exception message includes the elapsed / expected duration, the absolute drift, and (for budget overruns) the overshoot plus a grep-friendly (elapsed=Xms, budget=Yms, overrun=Zms) suffix for log scrapers. No Console.WriteLine debugging needed: every dimension you can assert on is also rendered in the failure message.
Full failure-diagnostics example, design notes, stability intent, and roadmap on GitHub.
Family
Part of an assertion family for TUnit:
- LogAssertions.TUnit
- SnapshotAssertions.TUnit
- MathAssertions.TUnit
- JsonAssertions.TUnit
- SseAssertions.TUnit
- GrpcAssertions.TUnit
License
MIT. Copyright (c) 2026 John Verheij.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.Extensions.TimeProvider.Testing (>= 10.7.0)
- TimeAssertions (>= 0.9.0)
- TUnit.Assertions (>= 1.53.0)
- TUnit.Core (>= 1.53.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
View the rendered release notes: https://github.com/JohnVerheij/TimeAssertions.TUnit/releases/tag/v0.9.0
Minor release. Adds fire observation to `ObservableTimeProvider`: a count of how many times timer callbacks ran, next to the existing leak and pending-due assertions. Purely additive. Also corrects stale guidance in the docs.
### Added
- **`Assert.That(time).HasTimerFiredCount(int expected)`**, **`HasNoTimerFired()`**, and **`HasTimerFiredAtLeast(int count)`** assert how many times timer callbacks fired across every timer the provider created. `ObservableTimeProvider` now wraps each timer callback to count its fires. The count is cumulative and is not reset on disposal. With a `FakeTimeProvider`, a fire is counted each time test code advances fake time past a due or period boundary, which keeps the count deterministic. `ObservableTimeProvider.TimerFireCount` (cumulative) and `ActiveTimerInfo.TimesFired` (per timer) expose the same data on the framework-agnostic core.
- `ObservableTimeProvider.CreateTimer` now validates that the callback is non-null up front (`ArgumentNullException`), matching the rest of the surface, instead of deferring to the inner provider.
### Fixed
- Once a periodic timer fires, `NextTimerDueTime` (and the `HasNextTimerDueApproximately` / `HasPendingTimerDueWithin` assertions) now report the timer's period, since that is when the next callback is due. A fired one-shot timer is disabled and drops out of the pending-due calculation. Previously the due time stayed frozen at its creation value until an explicit `Change`.
- Corrected stale documentation that claimed the package "deliberately does not ship its own polling assertion": it has shipped `HasNoActiveTimersEventually` and its count-targeted siblings since `0.7.0`/`0.8.0`. The README and the `ActiveTimerAssertions` XML docs now point to those overloads for the asynchronous disposal race, reserving generic `Eventually` for arbitrary conditions.
### Changed
- Bumped `PackageValidationBaselineVersion` from `0.7.0` to `0.8.1` on both packages so ApiCompat strict-mode validates `0.9.0` against the most recently published baseline. The new members are recorded as additive differences in `CompatibilitySuppressions.xml`.