|
| 1 | +# TUnit Assertion Reference |
| 2 | + |
| 3 | +All assertions are async and must be `await`-ed. |
| 4 | + |
| 5 | +```csharp |
| 6 | +await Assert.That(actual).IsEqualTo(expected); |
| 7 | +``` |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +## Equality & comparison |
| 12 | + |
| 13 | +```csharp |
| 14 | +await Assert.That(actual).IsEqualTo(expected); |
| 15 | +await Assert.That(actual).IsNotEqualTo(unexpected); |
| 16 | +await Assert.That(number).IsGreaterThan(0); |
| 17 | +await Assert.That(number).IsGreaterThanOrEqualTo(1); |
| 18 | +await Assert.That(number).IsLessThan(100); |
| 19 | +await Assert.That(number).IsLessThanOrEqualTo(99); |
| 20 | +await Assert.That(number).IsBetween(1, 99); // inclusive |
| 21 | +``` |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +## Null & existence |
| 26 | + |
| 27 | +```csharp |
| 28 | +await Assert.That(value).IsNull(); |
| 29 | +await Assert.That(value).IsNotNull(); |
| 30 | +``` |
| 31 | + |
| 32 | +--- |
| 33 | + |
| 34 | +## Boolean |
| 35 | + |
| 36 | +```csharp |
| 37 | +await Assert.That(flag).IsTrue(); |
| 38 | +await Assert.That(flag).IsFalse(); |
| 39 | +``` |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +## Type checks |
| 44 | + |
| 45 | +```csharp |
| 46 | +await Assert.That(obj).IsTypeOf<MyType>(); |
| 47 | +await Assert.That(obj).IsNotTypeOf<WrongType>(); |
| 48 | +await Assert.That(obj).IsAssignableTo<IMyInterface>(); |
| 49 | +``` |
| 50 | + |
| 51 | +--- |
| 52 | + |
| 53 | +## Collections |
| 54 | + |
| 55 | +```csharp |
| 56 | +await Assert.That(list).IsEmpty(); |
| 57 | +await Assert.That(list).IsNotEmpty(); |
| 58 | +await Assert.That(list).Contains(item); |
| 59 | +await Assert.That(list).DoesNotContain(item); |
| 60 | +await Assert.That(list).Count().IsEqualTo(3); |
| 61 | +await Assert.That(list).HasSingleItem(); |
| 62 | +``` |
| 63 | + |
| 64 | +--- |
| 65 | + |
| 66 | +## Strings |
| 67 | + |
| 68 | +```csharp |
| 69 | +await Assert.That(text).IsEqualTo("exact"); |
| 70 | +await Assert.That(text).IsEmpty(); |
| 71 | +await Assert.That(text).IsNotEmpty(); |
| 72 | +await Assert.That(text).Contains("sub"); |
| 73 | +await Assert.That(text).DoesNotContain("absent"); |
| 74 | +await Assert.That(text).StartsWith("prefix"); |
| 75 | +await Assert.That(text).EndsWith("suffix"); |
| 76 | +await Assert.That(text).Matches(@"^\d{4}-\d{2}-\d{2}$"); // regex |
| 77 | +``` |
| 78 | + |
| 79 | +--- |
| 80 | + |
| 81 | +## Exceptions |
| 82 | + |
| 83 | +```csharp |
| 84 | +// Synchronous throw wrapped in a lambda |
| 85 | +await Assert.ThrowsAsync<ArgumentException>(() => |
| 86 | +{ |
| 87 | + SomeMethod(); |
| 88 | + return Task.CompletedTask; |
| 89 | +}); |
| 90 | + |
| 91 | +// Async throw |
| 92 | +await Assert.ThrowsAsync<InvalidOperationException>(async () => |
| 93 | + await SomeAsyncMethod()); |
| 94 | + |
| 95 | +// Capture and inspect the exception |
| 96 | +var ex = await Assert.ThrowsAsync<ApiException>(async () => |
| 97 | + await client.GetBookAsync(id)); |
| 98 | +await Assert.That(ex!.StatusCode).IsEqualTo(HttpStatusCode.NotFound); |
| 99 | + |
| 100 | +// Verify NO exception is thrown (rarely needed — just call the method directly) |
| 101 | +await Assert.DoesNotThrowAsync(async () => await SafeMethod()); |
| 102 | +``` |
| 103 | + |
| 104 | +--- |
| 105 | + |
| 106 | +## Multiple assertions grouped (Assert.Multiple) |
| 107 | + |
| 108 | +Use `Assert.Multiple()` to run several assertions and collect all failures before |
| 109 | +reporting, rather than stopping at the first failure. |
| 110 | + |
| 111 | +```csharp |
| 112 | +[Test] |
| 113 | +public async Task Constructor_SetsAllProperties() |
| 114 | +{ |
| 115 | + var date = new PartialDate(2023, 5, 15); |
| 116 | + |
| 117 | + using var scope = Assert.Multiple(); |
| 118 | + await Assert.That(date.Year).IsEqualTo(2023); |
| 119 | + await Assert.That(date.Month).IsEqualTo(5); |
| 120 | + await Assert.That(date.Day).IsEqualTo(15); |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +> `Assert.Multiple()` returns an `IDisposable`; always wrap in `using var scope`. |
| 125 | +
|
| 126 | +--- |
| 127 | + |
| 128 | +## Chained member assertions (single await, multiple properties) |
| 129 | + |
| 130 | +```csharp |
| 131 | +await Assert.That(user) |
| 132 | + .IsNotNull() |
| 133 | + .And.Member(u => u.Email, e => e.IsEqualTo("john@example.com")) |
| 134 | + .And.Member(u => u.Age, a => a.IsGreaterThan(18)); |
| 135 | +``` |
| 136 | + |
| 137 | +--- |
| 138 | + |
| 139 | +## Assert.Fail |
| 140 | + |
| 141 | +Use when a code path should never be reached: |
| 142 | + |
| 143 | +```csharp |
| 144 | +Assert.Fail("Expected an exception, but none was thrown."); |
| 145 | +``` |
| 146 | + |
| 147 | +--- |
| 148 | + |
| 149 | +## Awaiting assertions — why it matters |
| 150 | + |
| 151 | +TUnit returns a lazy assertion object from `Assert.That(…)`. The assertion is only |
| 152 | +evaluated when `await`-ed. Missing the `await` means the check is never run and |
| 153 | +the test passes silently even when the code is wrong. This is the most common |
| 154 | +mistake when migrating from xUnit/NUnit. |
0 commit comments