TestManagementHttpClient创建测试运行

时间:2016-05-26 17:07:33

标签: tfs visual-studio-test-runner

我正在尝试使用C#代码创建TFS测试运行。但每次我都低于错误。虽然我给出了测试计划的详细信息。我甚至找不到关于此的文件。

错误

类型' Microsoft.TeamFoundation.TestManagement.WebApi.TestObjectNotFoundException'发生在mscorlib.dll中但未在用户代码中处理

其他信息:未找到测试计划{0}。

代码

    public async Task CreateTestRun()
    {
        TestManagementHttpClient witClient = connection.GetClient<TestManagementHttpClient>();
        TestCaseResultUpdateModel TestCaseResultUpdateModel = new TestCaseResultUpdateModel();
        ShallowReference testPlanReference = new ShallowReference();
        testPlanReference.Id = "{TestPlanId}";
        testPlanReference.Name = "{TestPlanName}";
        testPlanReference.Url = "http://{TFSInstance}/{Project}/_apis/test/plans/{TestPlanID}";
        RunCreateModel RunCreateModel = new RunCreateModel("SWAT-Run","",new int[] {2304187},testPlanReference,
                                                            null,0,"",false,"Error Message","","","","","comment","","", "",
                                                            null, null, null, null,null,"","","","",new TimeSpan(0,0,10,0,0),"",null);
        TestRun testRun = await witClient.CreateTestRunAsync(this.Project, RunCreateModel, null);
    }

1 个答案:

答案 0 :(得分:0)

我已经使用以下代码完成了它,希望它对您有所帮助。

    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);
相关问题