c# - 如何使用 Team Foundation Server API 创建测试运行和结果?

标签 c# visual-studio-2010 tfs-sdk microsoft-test-manager

我找到了几个关于使用 TFS API 检索测试结果的示例,但没有关于以编程方式创建结果的文档。我的目标是创建一个轻量级的替代方案来使用 Microsoft 测试管理器来运行手动测试。有人对此有经验吗?有没有我遗漏的例子?

这是我目前所拥有的:

ITestCaseResult CreateNewTestCaseResult(ITestSuiteEntry testCaseEntry)
{
    var run = testCaseEntry.TestSuite.Plan.CreateTestRun(false /* not automated */);
    run.AddTest(testCaseEntry.TestCase.Id, suiteEntry.TestSuite.DefaultConfigurations[0].Id, suiteEntry.TestSuite.Plan.Owner);
    run.Save(); // so that results object is created
    return run.QueryResults()[0];
}

我不确定这是否是启动新运行的正确方法,我也不确定如何记录测试的每个操作的结果。

最佳答案

2012 年 8 月 15 日更新:

下面的示例现已集成到我的开源 TFS 测试步骤编辑器工具中。在最新版本中,它获得了将测试结果发布到 TFS 的能力。请参阅 GitHub 上的来源.


我现在有了发布测试结果的工作代码。请注意,以下代码接受 ITestPoint(这代表特定套件中的测试用例)并且有一些我的内部类(不包括在内)只为每个步骤提供结果和附件路径。

var tfsRun = _testPoint.Plan.CreateTestRun(false);

tfsRun.DateStarted = DateTime.Now;
tfsRun.AddTestPoint(_testPoint, _currentIdentity);
tfsRun.DateCompleted = DateTime.Now;
tfsRun.Save(); // so results object is created

var result = tfsRun.QueryResults()[0];
result.Owner = _currentIdentity;
result.RunBy = _currentIdentity;
result.State = TestResultState.Completed;
result.DateStarted = DateTime.Now;
result.Duration = new TimeSpan(0L);
result.DateCompleted = DateTime.Now.AddMinutes(0.0);

var iteration = result.CreateIteration(1);
iteration.DateStarted = DateTime.Now;
iteration.DateCompleted = DateTime.Now;
iteration.Duration = new TimeSpan(0L);
iteration.Comment = "Run from TFS Test Steps Editor by " + _currentIdentity.DisplayName;

for (int actionIndex = 0; actionIndex < _testEditInfo.TestCase.Actions.Count; actionIndex++)
{
    var testAction = _testEditInfo.TestCase.Actions[actionIndex];
    if (testAction is ISharedStepReference)
        continue;

    var userStep = _testEditInfo.SimpleSteps[actionIndex];

    var stepResult = iteration.CreateStepResult(testAction.Id);
    stepResult.ErrorMessage = String.Empty;
    stepResult.Outcome = userStep.Outcome;

    foreach (var attachmentPath in userStep.AttachmentPaths)
    {
        var attachment = stepResult.CreateAttachment(attachmentPath);
        stepResult.Attachments.Add(attachment);
    }

    iteration.Actions.Add(stepResult);
}

var overallOutcome = _testEditInfo.SimpleSteps.Any(s => s.Outcome != TestOutcome.Passed)
    ? TestOutcome.Failed
    : TestOutcome.Passed;

iteration.Outcome = overallOutcome;

result.Iterations.Add(iteration);

result.Outcome = overallOutcome;
result.Save(false);

关于c# - 如何使用 Team Foundation Server API 创建测试运行和结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6505812/

相关文章:

c# - 仅在 C# 中的特定时间触发事件

c# - 将 DataTable 分配给 DataGridView 时的 "NullReferenceException was unhandled"

c++ - bad_alloc 标识符无法识别?

.net - 在 TFS 2010 中是否有任何等效于报告者(或更改 "Created By"字段的方法)

tfs - WorkItem.Save() 是如何工作的?

c# - 安全 LDAP 身份验证问题

C# 复选框列出选定的 Items.Text 到 Labels.Text

c# - 使用 `UseUrl(...)` 配置文件中的设置?

C#:如何对依赖于同一类中另一个方法的方法进行单元测试?

c++ - 是否可以使用 C++( native )访问 TFS API