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
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
private static TestContext _testContext;
[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
{
_testContext = testContext;
}
Usage of Timer
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