XiHan.Framework.Testing 5.0.0

Prefix Reserved
dotnet add package XiHan.Framework.Testing --version 5.0.0
                    
NuGet\Install-Package XiHan.Framework.Testing -Version 5.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="XiHan.Framework.Testing" Version="5.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="XiHan.Framework.Testing" Version="5.0.0" />
                    
Directory.Packages.props
<PackageReference Include="XiHan.Framework.Testing" />
                    
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 XiHan.Framework.Testing --version 5.0.0
                    
#r "nuget: XiHan.Framework.Testing, 5.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 XiHan.Framework.Testing@5.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=XiHan.Framework.Testing&version=5.0.0
                    
Install as a Cake Addin
#tool nuget:?package=XiHan.Framework.Testing&version=5.0.0
                    
Install as a Cake Tool

XiHan.Framework.Testing

关于

XiHan.Framework.Testing 是测试基座:没有 HTTP 的内存测试宿主、可注入行为的特性桩、按能力 ID 文本构造清单与独立调用上下文的工具。供框架、提供程序与应用的测试项目引用,也是各一致性套件装配宿主的基础。

主要特性

  • TestHost 在内存里装配并启动应用,调用直接走完整管道
  • StubFeature 注入配置、启动、停止行为并记录事件
  • 不依赖宿主就能构造调用上下文
  • 内存里的通行密钥认证器,按 WebAuthn 格式生成可篡改的响应
  • FaultyCache:可以随时切换成「缓存后端不可达」的 ICache 包装,测组件在缓存故障时的行为
  • ScriptedUnitOfWork 与 ScriptedRepository<TEntity, TKey>:按脚本在开始、读写、提交、回滚、释放上失败的替身,测自己开工作单元的代码在数据库故障时怎样收尾
  • LogCapture:把日志收进内存的日志工厂,按级别、类别、消息与异常断言组件写了哪条日志
  • SpanCapture 与 MetricCapture:按源名与 Meter 名监听,把 Span、采样器被询问的创建与测量值收进内存,有条数上限,按 trace-id 或异步流只取本用例的

如何使用

安装

dotnet add package XiHan.Framework.Testing

使用

在测试里装配特性、读配置、走一次管道:

var feature = new StubFeature(TestManifests.Create("xihan.test.stub", requires: ["kernel.hosting@1"]));
await using var host = await TestHost.StartAsync(
    b => b.UseFeature(feature),
    configuration: [new KeyValuePair<string, string?>("XiHan:Kernel:StartTimeoutSeconds", "5")]);

var result = await host.InvokeAsync(static context =>
{
    context.SetOutcome(Result.Success());
    return ValueTask.CompletedTask;
});

在装配特性之前把 FaultyCache 登记为 ICache 单例,测试中途让缓存后端「断线」:

var cache = new FaultyCache(innerCache);
await using var host = await TestHost.StartAsync(b => b.UseFeature(feature), services: s => s.AddSingleton<ICache>(cache));

// 之后每个 ICache 操作都抛出携带 CACHING_UNAVAILABLE_001 的 FrameworkException
cache.Failing = true;

几个宿主登记同一个实例时共用一份缓存,可以模拟多实例共用的缓存后端。

管道外自己开工作单元的代码(单例存储、后台循环)用两个替身测收尾:数据库断开时读取抛出、回滚再抛,结果不该被回滚的异常盖掉。

var unitOfWork = new ScriptedUnitOfWork { RollbackFailure = new InvalidOperationException("The connection is closed.") };
await using var services = new ServiceCollection()
    .AddScoped<IUnitOfWork>(_ => unitOfWork)
    .AddScoped<IRepository<Order, Guid>>(_ => new ScriptedRepository<Order, Guid> { Failure = new TimeoutException() })
    .BuildServiceProvider();

// 跑被测代码之后:unitOfWork.Calls 是 ["begin", "rollback", "dispose"]

断言组件写了哪条日志:把 LogCapture 登记为 ILoggerFactory,或直接从它取日志器交给被测对象。

using var logs = new LogCapture();
var purger = new OrderPurger(services, TimeProvider.System, logs.CreateLogger<OrderPurger>());

await purger.PurgeAsync("acme", CancellationToken.None);

Assert.Contains(logs.Entries, e => e.Level == LogLevel.Warning && e.Exception is TimeoutException);

断言组件开了哪条 Span、记了哪个指标:监听器是进程级的,Span 按本用例的 trace-id 取,指标限定到本用例的异步流。

using var spans = new SpanCapture([FrameworkDiagnostics.SourceName]);
using var metrics = new MetricCapture([FrameworkDiagnostics.SourceName]);
metrics.RestrictToCurrentFlow();

var response = await invoker.InvokeAsync(context, request);

var span = Assert.Single(spans.Spans(context.TraceId));
Assert.Single(metrics.Entries, m => m.Instrument == "xihan.invocations");

主要类型

此库提供的主要类型:

  • TestHost:内存测试宿主
  • StubFeature:可注入行为并记录事件的特性桩
  • TestManifests:用能力 ID 文本构造清单
  • TestInvocationContext:不依赖宿主的调用上下文
  • WebAuthnTestAuthenticator:测试用的通行密钥认证器,生成注册与认证响应
  • FaultyCache:可切换成缓存后端不可达的 ICache 包装
  • ScriptedUnitOfWork:按脚本失败并记录调用顺序的工作单元替身
  • ScriptedRepository<TEntity, TKey>:按脚本返回或抛出的仓储替身
  • LogCapture、LogCaptureEntry:收日志的工厂与一条日志(级别、类别、消息、异常)
  • SpanCapture、SpanSamplingRequest:收 Span 的监听器与采样器被询问的一次创建(源、名字、种类、trace-id、创建时的标签)
  • MetricCapture、MetricCaptureEntry:收测量值的监听器与一个测量值(Meter、仪表、值、标签)

更多文档

反馈与贡献

XiHan.Framework.Testing 以 MIT 许可证 开源。欢迎到 GitHub 仓库 提交问题和贡献。

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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 (3)

Showing the top 3 NuGet packages that depend on XiHan.Framework.Testing:

Package Downloads
XiHan.Framework.Persistence.Conformance

曦寒框架持久化一致性测试套件

XiHan.Framework.Localization.Conformance

曦寒框架本地化一致性测试套件

XiHan.Framework.Messaging.Conformance

曦寒框架消息一致性测试套件

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
5.0.0 212 12/1/2026
5.0.0-rc.9 83 11/30/2026
5.0.0-rc.8 146 11/28/2026
5.0.0-rc.7 137 11/23/2026
5.0.0-rc.6 225 11/21/2026
5.0.0-rc.5 93 11/20/2026
5.0.0-rc.4 82 11/19/2026
5.0.0-rc.3 75 11/19/2026
5.0.0-rc.2 90 11/15/2026
5.0.0-rc.1 153 11/13/2026
5.0.0-preview.3 90 11/11/2026
5.0.0-preview.2 108 11/6/2026
5.0.0-preview.1 78 11/4/2026