0

I am trying below C# code to create TFS test run. But every time I am getting below error. Though I have given test plan details. I couldnt even find documentations on this.

Error

An exception of type 'Microsoft.TeamFoundation.TestManagement.WebApi.TestObjectNotFoundException' occurred in mscorlib.dll but was not handled in user code

Additional information: Test plan {0} not found.

Code

    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);
    }
mfulton26
  • 29,956
  • 6
  • 64
  • 88
Manivas
  • 11
  • 3

1 Answers1

0

I have done it succssful with below code, hope it can be helpful to you.

    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);
John.s
  • 3
  • 1