ZeroAlloc.TestHelpers 1.10.0

dotnet add package ZeroAlloc.TestHelpers --version 1.10.0
                    
NuGet\Install-Package ZeroAlloc.TestHelpers -Version 1.10.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="ZeroAlloc.TestHelpers" Version="1.10.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ZeroAlloc.TestHelpers" Version="1.10.0" />
                    
Directory.Packages.props
<PackageReference Include="ZeroAlloc.TestHelpers">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
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 ZeroAlloc.TestHelpers --version 1.10.0
                    
#r "nuget: ZeroAlloc.TestHelpers, 1.10.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 ZeroAlloc.TestHelpers@1.10.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=ZeroAlloc.TestHelpers&version=1.10.0
                    
Install as a Cake Addin
#tool nuget:?package=ZeroAlloc.TestHelpers&version=1.10.0
                    
Install as a Cake Tool

ZeroAlloc.TestHelpers

NuGet Build codecov License: MIT GitHub Sponsors

Prove "zero allocation" in a unit test: exact per-call allocation budgets that fail CI when a hot path regresses, plus snapshot testing for source generators, shipped as source with no runtime dependency.

Why ZeroAlloc.TestHelpers

  • A claim becomes a test. AllocationGate runs a call as many times as you ask and fails if it allocates more bytes per call than you allow. A BenchmarkDotNet [MemoryDiagnoser] run shows a number; this gate fails CI when the number changes.
  • Exact counts. Synchronous gates count with GC.GetAllocatedBytesForCurrentThread after a forced GC and a warmup, and round the average up, so a recurring allocation never reads as 0 B.
  • Steady state, not first runs. Each gate measures windows of calls until two in a row agree and keeps the least, so a one-off allocation is not counted. A window that is not yet within budget does not end the gate until tiered compilation has settled, so unoptimised tier-0 code, which can box where the optimised code does not, never fails a budget. A passing gate stops after two windows. A measurement that cannot settle within 20 s throws instead of returning a figure that may be tier-0.
  • Async calls too. The Async gates await every call and count allocations on every thread, for code that really yields on I/O.
  • Source-only, nothing to ship. The helpers compile into your test project as internal classes: no runtime DLL, nothing on your public API, and AllocationGate compiles into a NativeAOT app; CI publishes one with it.

Install

dotnet add package ZeroAlloc.TestHelpers

The package is a development dependency, so it stays private to the project that references it.

Quick start

using Xunit;
using ZeroAlloc.TestHelpers;

public class FormattingTests
{
    private readonly char[] _buffer = new char[16];

    [Fact]
    public void TryFormat_into_a_buffer_allocates_nothing() =>
        AllocationGate.AssertBudget(0, 1_000, () => 12_345.TryFormat(_buffer, out _), "TryFormat");

    [Fact]
    public void ToString_allocates_exactly_one_string() =>
        Assert.Equal(32, AllocationGate.MeasureBytesPerCall(1_000, () => 12_345.ToString()));
}

Put 12_345.ToString() under the zero budget and the test fails with the exact cost:

AllocationGate: ToString allocated 32000 B total over 1000 iterations (32 B/call avg, the least of 59 windows), budget is 0 B/call (0 B total).

What you get

  • Budgets and measurements for Action and ValueTask<T> calls: AssertBudget, MeasureBytesPerCall and their ValueTask variants. AllocationGate
  • Relative gates: AssertNoMoreThan fails when a candidate path allocates more than a baseline measured in the same run.
  • Async gates with a documented method and tolerance, for calls that complete on another thread. Calls that complete asynchronously
  • TextSnapshot for string snapshots, with no Roslyn reference. Snapshot testing
  • GeneratorSnapshot for source-generator output, reading and writing the Verify.SourceGenerators file format, so existing *.verified.cs files keep working. It is compiled only into projects that reference Roslyn. Installation and packaging

Learn more

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.10.0 4,504 11/6/2026
1.9.0 1,896 10/30/2026
1.8.2 523 10/30/2026
1.8.1 421 10/29/2026
1.8.0 2,668 10/17/2026
1.7.3 1,468 10/17/2026
1.7.2 1,120 10/16/2026
1.7.1 892 10/15/2026
1.7.0 1,231 10/12/2026
1.6.2 401 10/12/2026
1.6.1 1,119 10/11/2026
1.6.0 1,341 10/10/2026
1.5.1 1,015 10/7/2026
1.5.0 527 10/2/2026
1.4.0 385 10/1/2026
1.3.3 3,472 9/24/2026
1.3.2 833 9/20/2026
1.3.1 407 9/20/2026
1.3.0 187 9/20/2026
1.2.3 2,615 9/19/2026
Loading failed