有没有一种方法可以计算测试方法的经过时间而忽略初始化所花费的时间? [英] Is there a way to calculate elapsed time for test methods ignoring time spent initializing?

查看:56
本文介绍了有没有一种方法可以计算测试方法的经过时间而忽略初始化所花费的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在StackOverflow和SuperUser之间出现灰色区域的问题.我怀疑答案可能是与代码相关的解决方案,例如创造性地使用 StopWatch ,因此我将在这里提出.

This is the kind of question that treads the gray area between StackOverflow and SuperUser. As I suspect the answer is likely to involve code-related solutions, such as creative use of StopWatch, I'll ask it here.

我正在使用Visual Studio 2013对依赖于实体框架数据模型的控制器执行单元测试.测试资源管理器"很好地在简短列表中显示了我的测试结果及其经过的时间.

I am using Visual Studio 2013 to perform unit testing against controllers that depend on an entity framework data model. The Test Explorer nicely presents my test results as well as their elapsed time in a short list.

但是,我发现我的控制器单元测试花费的时间比我预期的长得多.我开始怀疑这是由于我用来创建模拟(使用Moq,但这没关系)实体模型的初始化代码.

I discovered, though, that my Controller unit tests were taking considerably longer than I expected. I began to suspect this was due to the initialization code that I use to create a mocked (using Moq, but that doesn't matter) entity model.

果然,证明初始化例程已包含在经过的时间中是一件小事.

Sure enough, it was a small matter to show that initialization routines are included in the elapsed time.

[TestClass]
public class InitializeTest
{
    [TestInitialize]
    public void Initialize()
    {
        Thread.Sleep(10000);
    }

    [TestMethod]
    public void TestInitializeRuntime()
    {
        Assert.Inconclusive();
    }
}

这在测试浏览器中产生了以下输出:

This produced the following output in the test explorer:

这使我的模拟实体模型支持的测试经过时间相当无用,因为初始化代码通常消耗的时间超过测试经过时间的95%.每种方法看起来都很慢,实际上却并非如此.

This renders the elapsed time of tests backed by my mocked entity model fairly non-useful, as initialization code generally consumes greater than 95% of the elapsed time of the test. Every method looks slow, when in fact they are not so.

是否存在替代配置或某些创造性的代码使用方式(例如,如前所述的 StopWatch ),这使我只能报告测试方法的经过时间,不包括初始化或调试所花费的时间.清理测试?

Is there either an alternative configuration or some creative use of code (such as StopWatch, as mentioned earlier) that will allow me to report the elapsed time of test methods only, excluding time spent initializing or cleaning up the tests?

推荐答案

我今天发现了一种在MSTest中处理昂贵的初始化而又不会导致测试报告缓慢的方法.我发布此答案供考虑但不接受,因为它确实具有适度的代码味道.

I discovered today a method of handling expensive initialization in MSTest without it causing the tests to report as slow. I post this answer for consideration without accepting it because it does have a modest code smell.

MSTest每次运行测试时都会创建一个新的测试类实例.由于这种行为,实例构造函数中编写的代码每次测试都会发生一次.这种行为与 [TestInitialize] 方法类似,但有一个例外:MSTest在创建测试类的实例之后,在之后,在之前,开始计时单元测试.>执行 [TestInitialize] 例程.

MSTest creates a new instance of the test class each time it runs a test. Because of this behavior, code written in an instance constructor occurs once per test. This is similar behavior to the [TestInitialize] method with one exception: MSTest begins timing the unit test after creating the instance of the test class and before executing the [TestInitialize] routine.

这种特定于MSTest的行为的结果是,可以将初始化代码从自动生成的时序统计信息中省略,并将其放置在构造函数中.

As a result of this MSTest-specific behavior, one can put initialization code that should be omitted from the automatically-generated timing statistics in the constructor.

为演示我的意思,请考虑以下测试和生成的输出.

To demonstrate what I mean, consider the following test and generated output.

测试:

public class ConstructorTest
{
    public ConstructorTest()
    {
        System.Threading.Thread.Sleep(10000);
    }

    [TestMethod]
    public void Index()
    {
    }

    [TestMethod]
    public void About()
    {
    }
}

输出:

我的想法:

上面的代码肯定会产生我想要的效果;但是,尽管它看上去很安全,但可以使用构造函数或 [TestInitialize] 方法来处理初始化,我必须假设后者有充分的理由存在于此框架中.

The code above certainly produces the effect I was looking for; however, while it appears safe to use either a constructor or a [TestInitialize] method to handle initialization, I must assume that the latter exists in this framework for a good reason.

有时可能会出现这样的情况,即在计算中包括初始化时间的报告可能会有用,例如在估计应消耗大量测试的实时时间时.

There might be a case made that reports including initialization time in their calculations might be useful, such as when estimating how much real time a large set of tests should be expected to consume.

Rich Turner关于时间敏感型操作应如何使用断言值得秒表的讨论也值得我们认识(并请我投票).另一方面,我将Visual Studio提供的自动生成的时序报告视为一个有用的工具,它可以识别即将失控的测试,而不必在每个测试中编写时序样板代码.

Rich Turner's discussion about how time sensitive operations deserve stop watches with assertions is also worth recognizing (and has my vote). On the other hand, I look at the automatically generated timing reports provided by Visual Studio as a useful tool to identify tests that are getting out of hand without having to author timing boilerplate code in every test.

总的来说,我很高兴找到一个解决方案,并对在此讨论的替代方案也表示赞赏.

In all, I am pleased to have found a solution and appreciate the alternatives discussed here as well.

干杯!

这篇关于有没有一种方法可以计算测试方法的经过时间而忽略初始化所花费的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆