Verify.Assertions

April 9, 2026 ยท View on GitHub

Discussions Build status NuGet Status

Extends Verify to allow assertion callbacks. This enables using assertion libraries to interrogate during serialization. The primary use case for this is when the data structures being verified are either complex or large.

See Milestones for release notes.

Sponsors

Entity Framework Extensions

Entity Framework Extensions is a major sponsor and is proud to contribute to the development this project.

Entity Framework Extensions

Developed using JetBrains IDEs

JetBrains logo.

NuGet

Enable

[ModuleInitializer]
public static void Init() =>
    VerifyAssertions.Initialize();

snippet source | anchor

Usage

Once enable, any assertion library can be used.

The below examples are simplistic for illustrating the usage. In a real world scenario, if data structures being verified are small, then the assertion can happen before or after the the Verify with no need to assert during serialization.

Xunit

[Fact]
public async Task XunitUsage()
{
    var nested = new Nested(Property: "value");
    var target = new Target(nested);
    await Verify(target)
        .Assert<Nested>(
            _ => Assert.Equal("value", _.Property));
}

snippet source | anchor

NUnit

[Test]
public async Task NUnitUsage()
{
    var nested = new Nested(Property: "value");
    var target = new Target(nested);
    await Verify(target)
        .Assert<Nested>(
            _ => Assert.That(_.Property, Is.EqualTo("value")));
}

snippet source | anchor

FluentAssertions

[Fact]
public async Task FluentAssertionsUsage()
{
    var nested = new Nested(Property: "value");
    var target = new Target(nested);
    await Verify(target)
        .Assert<Nested>(
            _ => _.Property.Should().Be("value"));
}

snippet source | anchor

Shouldly

[Fact]
public async Task ShouldlyUsage()
{
    var nested = new Nested(Property: "value");
    var target = new Target(nested);
    await Verify(target)
        .Assert<Nested>(
            _ => _.Property.ShouldBe("value"));
}

snippet source | anchor

Shared Assertions

Assertions can be added globally.

[ModuleInitializer]
public static void AddSharedAssert() =>
    VerifyAssertions
        .Assert<SharedNested>(
            _ => Assert.Equal("value", _.Property));

[Fact]
public async Task SharedAssert()
{
    var nested = new SharedNested(Property: "value");
    var target = new SharedTarget(nested);
    await Verify(target);
}

snippet source | anchor

Icon

Approval designed by Danang Marhendra from The Noun Project.