SharpAssert.Runtime 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package SharpAssert.Runtime --version 1.0.0
                    
NuGet\Install-Package SharpAssert.Runtime -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SharpAssert.Runtime" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SharpAssert.Runtime" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="SharpAssert.Runtime" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SharpAssert.Runtime --version 1.0.0
                    
#r "nuget: SharpAssert.Runtime, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SharpAssert.Runtime@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SharpAssert.Runtime&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=SharpAssert.Runtime&version=1.0.0
                    
Install as a Cake Tool

<p align="center"> <img src="https://raw.githubusercontent.com/yevhen/sharp.assert/refs/heads/main/logo.png" alt="SharpAssert logo"/> </p>

SharpAssert

A pytest inspired assertion library for .NET that provides detailed error reporting with no special syntax.

Overview

SharpAssert provides rich assertion diagnostics by automatically transforming your assertion expressions at compile time using MSBuild source rewriting, giving you detailed failure messages with powerful expression analysis.

using static Sharp;

var items = new[] { 1, 2, 3 };
var target = 4;

Assert(items.Contains(target));
// Assertion failed: items.Contains(target) at MyTest.cs:15
// items:  [1, 2, 3]  
// target: 4
// Result: false

Features

  • ๐Ÿ” Detailed Expression Analysis - See exactly why your assertions failed
  • โšก Zero Runtime Overhead - No reflection, no performance penalty
  • ๐Ÿ“ฆ Simple Setup - Just add NuGet package, no MSBuild configuration needed
  • ๐Ÿ”„ PowerAssert Integration - Complete support for PowerAssert (switch option)

Quick Start

1. Install Package

dotnet add package SharpAssert

2. Use SharpAssert in Your Tests

using static Sharp;

[Test]
public void Should_find_matching_item()
{
    var users = new[] { "Alice", "Bob", "Charlie" };
    var searchName = "David";
    
    Assert(users.Contains(searchName));
    // Assertion failed: users.Contains(searchName)
    // users: ["Alice", "Bob", "Charlie"]
    // searchName: "David"  
    // Result: false
}

How It Works

SharpAssert uses MSBuild source rewriting to automatically transform your assertion calls at compile time:

  1. You write: Assert(x == y)
  2. MSBuild rewrites: global::SharpAssert.SharpInternal.Assert(() => x == y, "x == y", "file.cs", 42)
  3. Runtime analysis: Expression tree provides detailed failure diagnostics when assertions fail
  4. Dual-world design: Original code preserved for IDE, rewritten code used for compilation

Advanced Usage

Complex Expression Analysis

var order = new Order { Items = new[] { "Coffee", "Tea" }, Total = 15.50m };
var expectedTotal = 12.00m;

Assert(order.Items.Length > 0 && order.Total == expectedTotal);
// Assertion failed: order.Items.Length > 0 && order.Total == expectedTotal
// order.Items.Length > 0 โ†’ True (Length: 2)
// order.Total == expectedTotal โ†’ False
//   order.Total:  15.50
//   expectedTotal: 12.00
// Result: False

Custom Error Messages

Assert(user.IsActive, $"User {user.Name} should be active for this operation");

Async-Safe Usage

// Works with async expressions
Assert(await GetBoolAsync()); // Rewritten to provide detailed analysis

Architecture

SharpAssert is built on modern .NET technologies:

  • MSBuild Source Rewriting - Compile-time code transformation
  • Roslyn Syntax Analysis - Advanced C# code parsing and generation
  • Expression Trees - Runtime expression analysis
  • CallerArgumentExpression - Fallback for edge cases
  • PowerAssert Backend - Automatic fallback for complex scenarios

PowerAssert Integration

SharpAssert includes PowerAssert as an intelligent fallback mechanism. When SharpAssert encounters expressions it doesn't yet fully support, it automatically delegates to PowerAssert to ensure you always get meaningful diagnostics. This happens transparently - you'll still get detailed error messages regardless of the underlying engine.

Note: Async/await and dynamic expressions currently use basic diagnostics via CallerArgumentExpression. Full support for these features is planned for future releases.

To force PowerAssert for all assertions (useful for comparison or debugging):

<PropertyGroup>
  <UsePowerAssert>true</UsePowerAssert>
</PropertyGroup>

Troubleshooting

Rewriter not working

  1. Verify SharpAssert package is installed (SharpAssert.Runtime comes automatically)
  2. Ensure using static Sharp; import

No detailed error messages

  1. Check build output contains: "SharpAssert: Rewriting X source files"
  2. Verify rewritten files exist in obj/Debug/net9.0/SharpRewritten/
  3. Ensure SharpInternal.Assert calls are being made (check generated code)
  4. Look for #line directives in generated files

Contributing

We welcome contributions! Please see our comprehensive Contributing Guide for:

  • ๐Ÿš€ Quick start guide for developers
  • ๐Ÿงช Testing strategy and workflow
  • ๐Ÿ“ฆ Package versioning best practices
  • ๐Ÿ”ง Development tips and debugging help
  • ๐Ÿ“ Commit guidelines and release process

License

MIT License

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on SharpAssert.Runtime:

Package Downloads
SharpAssert

A pytest inspired assertion library for .NET with no special syntax.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.6 133 8/13/2025
1.0.5 130 8/13/2025
1.0.4 131 8/13/2025
1.0.3 134 8/13/2025
1.0.0 137 8/13/2025