mercredi 4 février 2015

Is there a way to calculate elapsed time for test methods ignoring time spent initializing?

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.


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.


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:


Screenshot of Test Explorer


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.


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?


Aucun commentaire:

Enregistrer un commentaire