How can I accurately perform a test run in visual studio 2010

I have a set of unit tests for my project that I run using the visual studio test runner. I want to know how long the test takes when it starts. The test run parameters screen shows me the start and end time of the run, but only until the next second. My test suite currently takes less than a second to complete, so I can’t say if my set of 50 tests takes <0.1 seconds (good!) Or up to 1 second (bad!).

Is there any way to enable this level of accuracy?

+5
source share
3 answers

Your solutions root directory has a Test Results folder after test runs. If you look there, each run spits out a .TRX file with a timestamp of the test run as part of its name. Open this file and you will see that it is an XML file with a bunch of hard to read information about the test run. If you are looking for XML nodes "UnitTestResult", each of them will have the attribute "duration". This will show you how long each individual test took (assuming the test was run).

, , TRX, HTML- . , , , , .

, , , - . , , TRX .

: . 00: 00: 00.0015890 unit test.

+8

, .

(, ).

[TestClass]
public abstract class TestClassBase2
{
    protected TestContext testContextInstance;

    public TestContext TestContext
    {
        get { return testContextInstance; }
        set { testContextInstance = value; }
    }

    protected System.Diagnostics.Stopwatch _stopWatch;

    [TestInitialize]
    public void TestInitializeBase()
    {
        _stopWatch = System.Diagnostics.Stopwatch.StartNew();
    }


    [TestCleanup]
    public void TestCleanupBase()
    {
        System.Diagnostics.Debug.WriteLine("Done - Total Test time: {0} ms - {1}",
                                            _stopWatch.ElapsedMilliseconds, TestContext.TestName);
    }
}

[TestClass]
public class TempTests : TestClassBase2
{
    [TestMethod]
    public void TempTest()
    {
        // outputs: 
        // Done - Total Test time: 3 ms - TempTest
    }
}
+6

You can always run unit tests more times, and then divide the total time by the time they run. This will, in fact, be average, so if you are looking for low or high points, then this is not the best you can do.

+1
source

All Articles