65.9K
CodeProject 正在变化。 阅读更多。
Home

负载测试错误:传递给 BeginTimer 的计时器名称 '' 已经存在活动计时器

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1投票)

2016 年 11 月 3 日

CPOL
viewsIcon

7425

修复 TestContext.BeginTimer("TimerName") 的问题。

引言

如果没有正确设置 TestContext 实例,则可能会发生此错误。 简单来说,你需要一个 完整属性 用于 TestContext 实例,但你**不需要**为其设置值。

信息

如果你为 TestContext 创建一个属性并在 AssemblyInitializeClassInitialize 中设置其值,你就是在将你的属性赋值给一个 TestContext 实例。 如果你在负载测试中这样做,那么每个测试都会重用同一个测试上下文实例。 如果你使用 TestContext.BeginTimer("TimerName");,该 Context 实例会添加一个具有该名称的新 Timer,当下一个负载测试运行时,它会尝试将相同的计时器添加到相同的 TestContext,从而导致出现该错误。

正确设置

private TestContext _testContext;

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

使用此设置,TestContext 属性将由 TestExecutioner 设置,并且每个负载测试都将获得自己的 TestContext

错误设置

private static TestContext _testContext;

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

计时器用法

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

关注点

计时器将在测试结果中显示在场景 > 测试用例名称 > 事务 > 计时器名称下。

历史

  • 2016年11月3日:初始版本
© . All rights reserved.