首页 > 解决方案 > 如何在 Visual Studio 中仅测量单元测试的 ACT 部分?

问题描述

众所周知,单元测试由三个 A 组成:A rrange、A ct、A ssert。问题是 Visual Studio 测量所有三个 A 的单元测试经过的时间。

我确信它应该只测量动作时间。

那么是否可以只测量动作时间?

标签: visual-studiounit-testing

解决方案


可能如果您在测试中使用Stopwatch 类自己测量它。

[TestMethod]
public void Some_Test() {
    //Arrange
    //...
    Stopwatch stopWatch = new Stopwatch();

    //Act
    stopWatch.Start();
    //...
    stopWatch.Stop();

    //Assert        
    // Get the elapsed time as a TimeSpan value.
    TimeSpan ts = stopWatch.Elapsed;

    //...
}

问题是实际的测试适配器无法知道您在测试中的行为是什么,因此让视觉工作室能够实际测量它几乎是不可能的。


推荐阅读