Configs2026年7月21日·1 分钟阅读

FluentAssertions — Readable Test Assertions for .NET

FluentAssertions provides a rich set of extension methods for writing expressive, self-documenting assertions in .NET unit tests, with clear failure messages that tell you exactly what went wrong.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
FluentAssertions
直接安装命令
npx -y tokrepo@latest install 0e9271c0-8501-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

FluentAssertions replaces cryptic Assert.AreEqual(expected, actual) calls with natural-language expressions like actual.Should().Be(expected). When a test fails, the error message describes the mismatch in detail, saving time spent debugging why a test broke.

What FluentAssertions Does

  • Provides fluent .Should() assertions for strings, numbers, collections, dates, exceptions, and more
  • Generates detailed failure messages showing expected vs. actual values with context
  • Supports object graph comparison for deep equality checks between complex objects
  • Asserts on async operations, events, and execution time
  • Works with xUnit, NUnit, MSTest, and any .NET test framework

Architecture Overview

FluentAssertions is a pure .NET library that extends types with .Should() via extension methods. Each assertion returns a typed assertion object that chains further conditions with .And. When an assertion fails, it throws a framework-appropriate exception (xUnit's XunitException, NUnit's AssertionException, etc.) detected automatically at runtime. The library targets .NET Standard 2.0 and .NET 6+.

Self-Hosting & Configuration

  • Install via NuGet: dotnet add package FluentAssertions
  • No additional configuration needed for most use cases
  • Auto-detects the test framework (xUnit, NUnit, MSTest) at runtime
  • Customize assertion behavior with AssertionOptions.AssertEquivalencyUsing()
  • Add FluentAssertions.Analyzers for Roslyn-based best practice suggestions

Key Features

  • Natural-language assertion syntax that reads like specifications
  • Structural equivalency assertions for deep object comparison
  • Collection assertions: ContainSingle, BeInAscendingOrder, HaveElementAt, OnlyContain
  • Exception assertions: .Should().Throw<T>().WithMessage("...") and async variants
  • Execution time assertions: .Should().CompleteWithin(TimeSpan.FromSeconds(1))

Comparison with Similar Tools

  • Assert (xUnit/NUnit/MSTest) — built-in but terse; FluentAssertions adds readability and better failure messages
  • Shouldly — similar fluent syntax; FluentAssertions has broader type coverage and deeper object comparison
  • NFluent — fluent assertions with a different chaining style; smaller community
  • Verify (verify-tests) — snapshot-based assertions; FluentAssertions is value-based comparison
  • Chai (JavaScript) — JS fluent assertion library; FluentAssertions brings the same approach to .NET

FAQ

Q: Does FluentAssertions work with xUnit, NUnit, and MSTest? A: Yes. It auto-detects the test framework and throws the appropriate exception type for correct reporting.

Q: How do I compare two complex objects? A: Use actual.Should().BeEquivalentTo(expected) for structural comparison that checks property values recursively, ignoring property order.

Q: Can I customize which properties are compared? A: Yes. Pass options to BeEquivalentTo: actual.Should().BeEquivalentTo(expected, o => o.Excluding(x => x.Id)).

Q: Is FluentAssertions free? A: Yes. It is released under the Apache 2.0 license and free for all use.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产