Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 15, 2025

Description

The .Because() reason string now appears inline with the "Expected" line instead of at the bottom of assertion errors, eliminating the need to expand test output to see context.

Before:

Expected to be equal to "alpha2"
but found "alpha"

at Assert.That(config).IsEqualTo("alpha2").Because("groups should be self-contained")

because groups should be self-contained

After:

Expected to be equal to "alpha2", because groups should be self-contained
but found "alpha"

at Assert.That(config).IsEqualTo("alpha2").Because("groups should be self-contained")

Changes

  • Assertion<TValue>.CreateException(): Appends because message to expectation before formatting error message
  • AndAssertion/OrAssertion.BuildCombinedExpectation(): Includes because messages inline for each assertion in chains

Related Issue

Fixes #4118

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Performance improvement
  • Refactoring (no functional changes)

Checklist

Required

  • I have read the Contributing Guidelines
  • If this is a new feature, I started a discussion first and received agreement
  • My code follows the project's code style (modern C# syntax, proper naming conventions)
  • I have written tests that prove my fix is effective or my feature works

TUnit-Specific Requirements

  • Dual-Mode Implementation: If this change affects test discovery/execution, I have implemented it in BOTH:
    • Source Generator path (TUnit.Core.SourceGenerator) - N/A: assertion formatting only
    • Reflection path (TUnit.Engine) - N/A: assertion formatting only
  • Snapshot Tests: If I changed source generator output or public APIs:
    • I ran TUnit.Core.SourceGenerator.Tests and/or TUnit.PublicAPI tests
    • I reviewed the .received.txt files and accepted them as .verified.txt
    • I committed the updated .verified.txt files
  • Performance: If this change affects hot paths (test discovery, execution, assertions):
    • I minimized allocations and avoided LINQ in hot paths
    • I cached reflection results where appropriate
  • AOT Compatibility: If this change uses reflection:
    • I added appropriate [DynamicallyAccessedMembers] annotations
    • I verified the change works with dotnet publish -p:PublishAot=true

Testing

  • All existing tests pass (dotnet test)
  • I have added tests that cover my changes
  • I have tested both source-generated and reflection modes (if applicable)

Additional Notes

All 1,370 assertion tests pass. The existing BecauseTests suite validates behavior across simple assertions, And chains, and Or chains.

Original prompt

This section details on the original issue you should resolve

<issue_title>[Feature]: Add reason string to end of error message</issue_title>
<issue_description>### Problem Statement

It's frustrating having to expand the test output to see the reason string provided in the .Because() method.

Proposed Solution

I would like TUnit to append the reason string to the end of the assertion error message:

Image

Also, would be nice to have .Because() documented — maybe I am abusing it, or there is another mechanism I should be using?

Alternatives Considered

No response

Feature Category

Test Output / Reporting

How important is this feature to you?

Nice to have - would improve my experience

Additional Context

No response

Contribution

  • I'm willing to submit a pull request for this feature</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits December 15, 2025 16:14
Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
- Updated Assertion.CreateException to append because message to expectation line
- Updated AndAssertion and OrAssertion to include because messages inline
- Added comprehensive tests for new format
- All existing tests pass

Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
Copilot AI changed the title [WIP] Add reason string to end of assertion error messages Inline .Because() reason in assertion error messages Dec 15, 2025
Copilot AI requested a review from thomhurst December 15, 2025 16:41
@thomhurst thomhurst closed this Dec 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Add reason string to end of error message

2 participants