dotMemory Unit 3.2 Help

使用不受支持的单元测试框架

如果您使用的单元测试框架未被 dotMemory Unit 开箱即用地支持(请参阅 支持的框架列表 ),您可以通过使用 DotMemoryUnitController 类来提供支持。

您只需要告诉 dotMemory Unit 您的测试方法从哪里开始以及在哪里结束。 这是通过两个方法实现的: DotMemoryUnitController.TestStart()DotMemoryUnitController.TestEnd()。 我们建议您创建一个使用这些方法的 IDisposable 类*,然后使用创建此类实例的 using 语句将代码包装在您的测试中。

示例

internal class dotMemoryUnit : IDisposable { [MethodImpl(MethodImplOptions.NoInlining)] private dotMemoryUnit() { // frame1 is DotMemoryUnit.Support() method; frame2 is "test" method DotMemoryUnitController.TestStart(new StackTrace().GetFrame(2).GetMethod()); } [MethodImpl(MethodImplOptions.NoInlining)] public static IDisposable Support() { return new dotMemoryUnit(); } public void Dispose() { DotMemoryUnitController.TestEnd(); } } public class Tests { [SomeTestAttribute] // an attribute that specifies a test in the framework of your choice public void TestMethod1() { using (dotMemoryUnit.Support()) { ... // your code goes here } } }

DotMemoryUnitController 静态类

允许为任何单元测试框架提供 dotMemory Unit 支持。

QueryBuilder 方法

名称

描述

TestStart(MethodBase testMethod)

指示测试的开始。 testMethod 指定使用 dotMemory Unit 的测试方法。 请参阅上面的示例。

TestEnd()

指示测试的结束。 确保始终调用此方法(即使测试抛出异常)。

TestFailed(bool onMemoryAssert)

指示测试失败。 在处理测试中的异常时调用此方法。 使用 onMemoryAssert 指示测试是因“内存”断言失败还是由于其他原因失败。 默认情况下, onMemoryAsserttrue

仅当您使用快照 保存策略时才需要使用此方法。

重要提示! dotMemory.Check 在断言失败时会自行调用 TestFailed(true)

最后修改日期: 2025年 9月 28日