-
-
Notifications
You must be signed in to change notification settings - Fork 109
feat: enhance failure messages for All assertions to include value #4139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Code Review for PR #4139: Enhance failure messages for All assertionsSummaryThis PR successfully addresses issue #4137 by improving the error message when Review DetailsCode Quality: Excellent
Test Coverage: ExcellentThe PR includes 3 comprehensive test cases covering:
Test quality observations:
Potential Issues & SuggestionsMinor: Null item handlingIssue: The current implementation uses string interpolation Example scenario: var items = new string?[] { "Alice", null, "Bob" };
await Assert.That(items).All(x => x != null);
// Current: "item at index 1 with value [] does not satisfy predicate"
// Could be confused with empty stringSuggestion: Consider adding a test case for null items to verify the behavior is acceptable, or enhance message formatting for null values. Observation: Complex object ToString()For complex objects without custom
Security: No Concerns
Performance: No Concerns
Adherence to TUnit GuidelinesChecking against
Recommendation: APPROVE with suggestionThe PR is ready to merge with the current implementation. The suggested null handling improvement is optional and could be addressed in a follow-up if desired. Rationale:
Optional follow-up: Consider adding null item test case to verify behavior is acceptable, or enhance message formatting for null values. Final NotesGreat work on this improvement! The enhanced error messages will make debugging failed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR enhances the failure message for the All assertion on collections to include the actual value of the failing item, making it easier to diagnose test failures. Previously, the error message only indicated which index failed; now it shows both the index and the value.
Key Changes
- Enhanced error message format in
CollectionAllAssertionto include the failing item's value - Added comprehensive test coverage for the new error message format with multiple scenarios
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| TUnit.Assertions/Conditions/CollectionAssertions.cs | Updated error message in CollectionAllAssertion.CheckAsync to include with value [{item}] for better diagnostics |
| TUnit.Assertions.Tests/CollectionAssertionTests.cs | Added three test cases verifying the enhanced error messages work correctly for integers, strings (including empty), and multiple failing items |
| if (!_predicate(item)) | ||
| { | ||
| return Task.FromResult(AssertionResult.Failed($"item at index {index} does not satisfy predicate")); | ||
| return Task.FromResult(AssertionResult.Failed($"item at index {index} with value [{item}] does not satisfy predicate")); |
Copilot
AI
Dec 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message format uses square brackets [{item}] to display the value, but other similar error messages in this file use parentheses ({item}) format (see lines 796 and 852 in CollectionIsInOrderAssertion and CollectionIsInDescendingOrderAssertion). For consistency, consider using parentheses format: "item at index {index} ({item}) does not satisfy predicate" to match the existing pattern in the codebase.
| return Task.FromResult(AssertionResult.Failed($"item at index {index} with value [{item}] does not satisfy predicate")); | |
| return Task.FromResult(AssertionResult.Failed($"item at index {index} ({item}) does not satisfy predicate")); |
| [Test] | ||
| public async Task All_Predicate_Failure_Message_Contains_Index_And_Value() | ||
| { | ||
| var items = new[] { 2, 4, -5, 8 }; | ||
|
|
||
| await Assert.That(async () => | ||
| await Assert.That(items).All(x => x > 0) | ||
| ).Throws<AssertionException>() | ||
| .WithMessageContaining("index 2") | ||
| .And.WithMessageContaining("[-5]"); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task All_Predicate_Failure_Message_Contains_String_Value() | ||
| { | ||
| var names = new[] { "Alice", "Bob", "" }; | ||
|
|
||
| await Assert.That(async () => | ||
| await Assert.That(names).All(x => !string.IsNullOrEmpty(x)) | ||
| ).Throws<AssertionException>() | ||
| .WithMessageContaining("index 2") | ||
| .And.WithMessageContaining("[]"); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task All_Predicate_Failure_Message_Contains_First_Failing_Item() | ||
| { | ||
| var items = new[] { 1, 2, 3, -1, -2, -3 }; | ||
|
|
||
| await Assert.That(async () => | ||
| await Assert.That(items).All(x => x > 0) | ||
| ).Throws<AssertionException>() | ||
| .WithMessageContaining("index 3") | ||
| .And.WithMessageContaining("[-1]"); | ||
| } |
Copilot
AI
Dec 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message directly interpolates {item} which could display as an empty string if the item is null. Consider adding a test case for collections containing null values to verify the error message displays appropriately (e.g., "with value [null]" or similar).
Fixes #4137