Skip to content

Commit b2a1e5d

Browse files
committed
fix: standardize assertion error message formatting (#4867)
Standardize assertion error messages across TUnit.Assertions for consistency: - GetExpectation() now consistently uses "to [verb] [value]" pattern (e.g., "to match regex" instead of "text match regex") - Failure messages now consistently use "received [actual]" pattern instead of mixed "found", "value is", "was", "both values are" forms - Exception type names now consistently use .Name (short name) instead of .FullName (fully qualified name) Changes span 17 source files in TUnit.Assertions/Conditions and TUnit.Assertions/Collections, with corresponding updates to 10 test files.
1 parent 84b4edd commit b2a1e5d

27 files changed

Lines changed: 93 additions & 93 deletions

TUnit.Assertions.Tests/Assertions/Delegates/Throws.ExactlyTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public async Task Fails_For_Code_With_Other_Exceptions()
1010
{
1111
var expectedMessage = """
1212
Expected to throw exactly CustomException
13-
but threw TUnit.Assertions.Tests.Assertions.Delegates.Throws+OtherException
13+
but threw OtherException
1414
1515
at Assert.That(action).ThrowsExactly<CustomException>()
1616
""".NormalizeLineEndings();

TUnit.Assertions.Tests/Assertions/Delegates/Throws.OfTypeTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public async Task Fails_For_Code_With_Other_Exceptions()
1010
{
1111
var expectedMessage = """
1212
Expected to throw CustomException
13-
but threw TUnit.Assertions.Tests.Assertions.Delegates.Throws+OtherException
13+
but threw OtherException
1414
1515
at Assert.That(action).Throws<CustomException>()
1616
""".NormalizeLineEndings();
@@ -29,7 +29,7 @@ public async Task Fails_For_Code_With_Supertype_Exceptions()
2929
{
3030
var expectedMessage = """
3131
Expected to throw SubCustomException
32-
but threw TUnit.Assertions.Tests.Assertions.Delegates.Throws+CustomException
32+
but threw CustomException
3333
3434
at Assert.That(action).Throws<SubCustomException>()
3535
""".NormalizeLineEndings();

TUnit.Assertions.Tests/AsyncEnumerableAssertionTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public async Task Test_AsyncEnumerable_HasCount_Fails()
5454
var action = async () => await Assert.That(items).HasCount(5);
5555

5656
var exception = await Assert.That(action).Throws<AssertionException>();
57-
await Assert.That(exception.Message).Contains("found 3 items");
57+
await Assert.That(exception.Message).Contains("received 3 items");
5858
}
5959

6060
// Contains tests
@@ -72,7 +72,7 @@ public async Task Test_AsyncEnumerable_Contains_Fails()
7272
var action = async () => await Assert.That(items).Contains(99);
7373

7474
var exception = await Assert.That(action).Throws<AssertionException>();
75-
await Assert.That(exception.Message).Contains("item 99 was not found");
75+
await Assert.That(exception.Message).Contains("99 was not found in the collection");
7676
}
7777

7878
// DoesNotContain tests
@@ -90,7 +90,7 @@ public async Task Test_AsyncEnumerable_DoesNotContain_Fails()
9090
var action = async () => await Assert.That(items).DoesNotContain(5);
9191

9292
var exception = await Assert.That(action).Throws<AssertionException>();
93-
await Assert.That(exception.Message).Contains("item 5 was found");
93+
await Assert.That(exception.Message).Contains("5 was found in the collection");
9494
}
9595

9696
// All tests
@@ -157,7 +157,7 @@ public async Task Test_AsyncEnumerable_Null_Fails()
157157
var action = async () => await Assert.That(items!).IsEmpty();
158158

159159
var exception = await Assert.That(action).Throws<AssertionException>();
160-
await Assert.That(exception.Message).Contains("was null");
160+
await Assert.That(exception.Message).Contains("received null");
161161
}
162162

163163
// String async enumerable

TUnit.Assertions.Tests/Bugs/Tests2145.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ public async Task TestFailMessage()
1616

1717
var expectedMessage = """
1818
Expected to be equal to "world"
19-
but found "hello"
19+
but received "hello"
2020
2121
at Assert.That(val).IsEqualTo("world")
2222
2323
Expected to be equal to "World"
24-
but found "hello"
24+
but received "hello"
2525
2626
at Assert.That(val).IsEqualTo("World")
2727
""";

TUnit.Assertions.Tests/Helpers/StringDifferenceTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public async Task Works_For_Empty_String_As_Actual()
77
{
88
var expectedMessage = """
99
Expected to be equal to "some text"
10-
but found ""
10+
but received ""
1111
1212
at Assert.That(actual).IsEqualTo(expected)
1313
""".NormalizeLineEndings();
@@ -26,7 +26,7 @@ public async Task Works_For_Empty_String_As_Expected()
2626
{
2727
var expectedMessage = """
2828
Expected to be equal to ""
29-
but found "actual text"
29+
but received "actual text"
3030
3131
at Assert.That(actual).IsEqualTo(expected)
3232
""".NormalizeLineEndings();
@@ -45,7 +45,7 @@ public async Task Works_When_Actual_Starts_With_Expected()
4545
{
4646
var expectedMessage = """
4747
Expected to be equal to "some text"
48-
but found "some"
48+
but received "some"
4949
5050
at Assert.That(actual).IsEqualTo(expected)
5151
""".NormalizeLineEndings();
@@ -64,7 +64,7 @@ public async Task Works_When_Expected_Starts_With_Actual()
6464
{
6565
var expectedMessage = """
6666
Expected to be equal to "some"
67-
but found "some text"
67+
but received "some text"
6868
6969
at Assert.That(actual).IsEqualTo(expected)
7070
""".NormalizeLineEndings();

TUnit.Assertions.Tests/MemberCollectionAssertionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public async Task Member_Array_HasCount_Fails()
3030
await Assert.That(obj).Member(x => x.Tags, tags => tags.HasCount(5)));
3131

3232
await Assert.That(exception.Message).Contains("to have count 5");
33-
await Assert.That(exception.Message).Contains("but found 2");
33+
await Assert.That(exception.Message).Contains("but received 2");
3434
}
3535

3636
[Test]

TUnit.Assertions.Tests/Old/StringEqualsAssertionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ Volutpat vero est ea clita clita magna dolor nulla ipsum aliquyam nonumy.
153153
var exception = await TUnitAssert.ThrowsAsync<TUnitAssertionException>(async () => await TUnitAssert.That(value1).IsEqualTo(value2));
154154

155155
var expectedMessage = $"Expected to be equal to \"Lorem ipsum dolor sit amet diam duo amet sea rebum. Et voluptua ex voluptua no praesent diam eu se…\"{Environment.NewLine}" +
156-
$"but found \"Lorem ipsum dolor sit amet diam duo amet sea rebum. Et voluptua ex voluptua no praesent diam eu se…\" which differs at index 556:{Environment.NewLine}" +
156+
$"but received \"Lorem ipsum dolor sit amet diam duo amet sea rebum. Et voluptua ex voluptua no praesent diam eu se…\" which differs at index 556:{Environment.NewLine}" +
157157
$" ↓{Environment.NewLine}" +
158158
$" \"Consequat odio ea veniam. Amet enim in gubergren s…\"{Environment.NewLine}" +
159159
$" \"Consequat odio ea veniam! Amet enim in gubergren s…\"{Environment.NewLine}" +

TUnit.Assertions.Tests/Old/StringRegexAssertionTests.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public async Task Matches_WithInvalidPattern_StringPattern_Throws(Type exception
5858

5959
await TUnitAssert.That(exception!.Message.NormalizeLineEndings()).IsEqualTo(
6060
$"""
61-
Expected text match pattern
61+
Expected to match pattern
6262
but The regex "^\d+$" does not match with "{text}"
6363
6464
at Assert.That(text).Matches(pattern)
@@ -83,7 +83,7 @@ public async Task Matches_WithInvalidPattern_RegexPattern_Throws(Type exceptionT
8383

8484
await TUnitAssert.That(exception!.Message.NormalizeLineEndings()).IsEqualTo(
8585
$"""
86-
Expected text match pattern
86+
Expected to match pattern
8787
but The regex "^\d+$" does not match with "{text}"
8888
8989
at Assert.That(text).Matches(pattern)
@@ -112,7 +112,7 @@ public async Task Matches_WithInvalidPattern_GeneratedRegexPattern_Throws(Type e
112112

113113
await TUnitAssert.That(exception!.Message.NormalizeLineEndings()).IsEqualTo(
114114
$"""
115-
Expected text match regex
115+
Expected to match regex
116116
but The regex "^\d+$" does not match with "Hello123World"
117117
118118
at Assert.That(text).Matches(regex)
@@ -194,8 +194,8 @@ public async Task DoesNotMatch_WithInvalidPattern_StringPattern_Throws(Type exce
194194

195195
await TUnitAssert.That(exception!.Message.NormalizeLineEndings()).IsEqualTo(
196196
$"""
197-
Expected text to not match with pattern
198-
but The regex "^\d+$" matches with "{text}"
197+
Expected to not match pattern
198+
but received "{text}" which matches the pattern "^\d+$"
199199
200200
at Assert.That(text).DoesNotMatch(pattern)
201201
""".NormalizeLineEndings()
@@ -219,8 +219,8 @@ public async Task DoesNotMatch_WithInvalidPattern_RegexPattern_Throws(Type excep
219219

220220
await TUnitAssert.That(exception!.Message.NormalizeLineEndings()).IsEqualTo(
221221
$"""
222-
Expected text to not match with pattern
223-
but The regex "^\d+$" matches with "{text}"
222+
Expected to not match pattern
223+
but received "{text}" which matches the pattern "^\d+$"
224224
225225
at Assert.That(text).DoesNotMatch(pattern)
226226
""".NormalizeLineEndings()
@@ -248,8 +248,8 @@ public async Task DoesNotMatch_WithInvalidPattern_GeneratedRegexPattern_Throws(T
248248

249249
await TUnitAssert.That(exception!.Message.NormalizeLineEndings()).IsEqualTo(
250250
$"""
251-
Expected text to not match with regex
252-
but The regex "^\d+$" matches with "{text}"
251+
Expected to not match regex
252+
but received "{text}" which matches the pattern "^\d+$"
253253
254254
at Assert.That(text).DoesNotMatch(regex)
255255
""".NormalizeLineEndings()

TUnit.Assertions.Tests/SatisfiesTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ await Assert.That(myModel)
9292
).Throws<AssertionException>()
9393
.WithMessageMatching("""
9494
*to satisfy*
95-
*found "Hello"*
95+
*received "Hello"*
9696
""");
9797
}
9898

@@ -125,7 +125,7 @@ await Assert.That(myModel)
125125
.WithMessageMatching(
126126
"""
127127
*to satisfy*
128-
*found "Baz"*
128+
*received "Baz"*
129129
"""
130130
);
131131
}
@@ -144,7 +144,7 @@ await Assert.That(myModel)
144144
).Throws<AssertionException>()
145145
.WithMessageMatching("""
146146
*to satisfy*
147-
*found "Hello"*
147+
*received "Hello"*
148148
""");
149149
}
150150

@@ -176,7 +176,7 @@ await Assert.That(myModel)
176176
).Throws<AssertionException>()
177177
.WithMessageMatching(
178178
"""
179-
*found "Blah"*
179+
*received "Blah"*
180180
"""
181181
);
182182
}

TUnit.Assertions.Tests/ThrowInDelegateValueAssertionTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public async Task ThrowInDelegateValueAssertion_ReturnsExpectedErrorMessage()
77
{
88
var expectedContains = """
99
Expected to be equal to True
10-
but threw System.Exception
10+
but threw Exception
1111
""".NormalizeLineEndings();
1212
var assertion = async () => await Assert.That(() =>
1313
{
@@ -31,7 +31,7 @@ public async Task ThrowInDelegateValueAssertion_RespectsCaseInsensitiveMessage()
3131

3232
await Assert.That(assertion)
3333
.Throws<AssertionException>()
34-
.WithMessageContaining("SYSTEM.EXCEPTION", StringComparison.OrdinalIgnoreCase);
34+
.WithMessageContaining("EXCEPTION", StringComparison.OrdinalIgnoreCase);
3535
}
3636

3737
[Test]

0 commit comments

Comments
 (0)