Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / DevOps / load-testing

Load Testing Error: There is already an active timer with the name '' passed to BeginTimer

5.00/5 (1 vote)
3 Nov 2016CPOL 7.2K  
Fix for issue with TestContext.BeginTimer(TimerName);

Introduction

Without proper setup of a TestContext instance, this error can occur. For a quick overview, you need a full property for the TestContext instance, an you DO NOT need to set the value for it.

Information

If you create a property for TestContext and set the value in the AssemblyInitialize or ClassInitialize you are assigning your property to an instance of TestContext. If you do this in a load test, then every test reuses the same instance of that test context. If you use TestContext.BeginTimer("TimerName"); that instance of the Context adds a new Timer with that name, when the next load test runs it tries to add the same timer to the same TestContext and you get the error listed.

Proper Setup

C#
private TestContext _testContext;

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

With this setup, the TestContext property will be set from the TestExecutioner and each load test will get its own TestContext.

Bad Setup

C#
private static TestContext _testContext;

[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
{
    _testContext = testContext;
}

Usage of Timer

C#
TestContext.BeginTimer("DoWorkTimer");
DoWork();
TestContext.EndTimer("DoWorkTimer");

Points of Interest

Timers will show up in the test results under Scenario > Test Case Name > Transactions > Timer Name.

History

  • 3rd November, 2016: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)