使用 dotMemory Unit 独立启动器
如果您使用独立的单元测试运行器(而不是 Visual Studio 或 JetBrains Rider)运行测试,或者希望将内存测试作为持续集成构建的一部分,那么您需要一个独立的 dotMemory Unit 启动器—— dotMemoryUnit.exe 命令行工具。
dotMemoryUnit.exe 充当中介——它运行特定的独立单元测试运行器,并在运行的测试中提供对 dotMemory Unit 调用的支持。
dotMemory Unit 独立启动器以 zip 压缩包的形式分发,可在 dotMemory 页面下载。 dotMemory Unit NuGet 包也包含独立启动器。
有关可用工具参数的详细信息,请运行:
dotMemoryUnit.exe --help
使用 dotMemoryUnit.exe 运行测试
在最简单的情况下,您只需指定单元测试运行器的路径及其参数。 例如,这是使用 NUnit 和 dotnet 从文件(例如, MainTests.dll )运行测试的方法:
dotMemoryUnit.exe "C:\NUnit 2.6.4\bin\nunit-console.exe" -- "E:\MyProject\bin\Release\MainTests.dll"
dotMemoryUnit.exe "c:\Program Files\dotnet\dotnet.exe" -- test "E:\MyProject\bin\Release\MainTests.dll"
工具的输出包含有关成功和失败测试的数据。 例如:
...
Tests run: 3, Errors: 1, Failures: 0, Inconclusive: 0, Time: 28.3051788194675 seconds
Not run: 0, Invalid: 0, Ignored: 0, Skipped: 0
Errors and Failures:
1) Test Error : MainTests.IntegrationTests.Method2
AssertTrafficException : Allocated memory amount
Expected: 50,000,000
But was: 195,344,723
...
dotMemoryUnit.exe 退出代码
默认情况下,如果工具成功完成其工作,其退出代码为 0。 当您在 CI 服务器上运行工具时,这并不方便,因为您需要知道构建中是否有任何失败的测试。 在这种情况下,最佳选项是让 dotMemoryUnit.exe 返回您的单元测试运行器的退出代码。 为此,您应使用 --propagate-exit-code 参数。 例如:
dotMemoryUnit.exe "C:\NUnit 2.6.4\bin\nunit-console.exe" --propagate-exit-code -- "E:\MyProject\bin\Release\MainTests.dll"
请注意,目标可执行文件的参数必须始终放在命令的末尾。
最后修改日期: 2025年 9月 28日