TFS API: How to get attachments from shared steps?

486 Views Asked by At

This is what I see in the test results in the Test Manager: some attachments (1) are on the test steps (in this case - on a shared step). Some (2) are on a result level. In the report I'm working on, I can extract information about attachments (2), but fail to get information about (1). Any suggestions?

In the debugger I can see, that the steps are iterated, I can see, i.e. "Verify the stuff" string etc, but the step has 0 attachments.

enter image description here

For reference, this is most of the code I am using to extract test case results:

foreach (ITestCaseResult testCaseResult in resutlsToDisplay)
{
    project.TestCases.Find(test.Id);

    var testRun = testCaseResult.GetTestRun();
    var testPlanEntry = project.TestPlans.Find(testRun.TestPlanId);
    var artifacts = testCaseResult.QueryAssociatedWorkItemArtifacts();
    if (testPlanEntry.Name != testPlan)
        continue;

    var testResult = new TestResult // CUSTOM CLASS
    {
        TestConfiguration = testCaseResult.TestConfigurationName,
        TestRun = testRun.Id,
        DateCreated = testRun.DateCreated,
        DateStarted = testRun.DateStarted,
        DateCompleted = testRun.DateCompleted,
        Result = testCaseResult.Outcome.ToString(),
        TestResultComment = testRun.Comment,
        RunBy = testCaseResult.RunBy == null ? "" : testCaseResult.RunBy.DisplayName,
        Build = testRun.BuildNumber ?? ""
    };

    foreach (var iteration in testCaseResult.Iterations)
    {
        var iterationResult = testResult.Clone();
        iterationResult.ResultAttachmentHtml = getAttachments(iteration.Attachments, testCase.TestCaseId.ToString(), string.Empty, iteration.IterationId.ToString()); // HERE I GET THE ATTACHMENTS OF TYPE 2
        iterationResult.Iteration = iteration.IterationId;
        iterationResult.IterationComment = iteration.Comment;

        foreach (var step in steps)
        {
            var stepCopy = step.Clone();
            iterationResult.Steps.Add(stepCopy);
            var actionResult = iteration.FindActionResult(stepCopy.TestStep);
            if (actionResult == null)
                continue;

            stepCopy.Result.Comment = actionResult.ErrorMessage;
            stepCopy.Result.Result = actionResult.Outcome.ToString();
            stepCopy.Result.AttachmentHtml = getAttachments(actionResult.Attachments, testCase.TestCaseId.ToString(), step.Number, iteration.IterationId.ToString()); // HERE I DO NOT GET ATTACHMENTS OF TYPE 1 - WHY?
        }
        config.TestCaseResult.TestResults.Add(iterationResult);
    }
} //end foreach testCaseResult in resutlsToDisplay
1

There are 1 best solutions below

0
On BEST ANSWER

I think I figured it out eventually.

The ITestCaseResult contains ITestIterationResultCollection Iterations.

The ITestIterationResult contains TestActionResultCollection of ITestActionResult.

ITestActionResult can be either ITestStepResult or ISharedStepResult.

If it is ITestStepResult, then it has Attachments collection right there.

If it is ISharedStepResult, however, then it has its own TestActionResultCollection. So this is the one that I have to iterate to find if each member has Attachments.

The code to iterate over steps, including shared steps, looks like this:

foreach (ITestIterationResult iteration in testCaseResult.Iterations)
{
    var iterationResult = testResult.Clone();
    iterationResult.ResultAttachmentHtml = getAttachments(iteration.Attachments,
        testCase.TestCaseId.ToString(),
        string.Empty, iteration.IterationId.ToString());
    iterationResult.Iteration = iteration.IterationId;
    iterationResult.IterationComment = iteration.Comment;

    foreach (ITestActionResult action in iteration.Actions)
    {
        if (action is ITestStepResult)
        {
            GetStepResults(steps, action, iterationResult, iteration, getAttachments, testCase, false);
        }
        else if (action is ISharedStepResult)
        {
            ISharedStepResult result = action as ISharedStepResult;
            foreach (var sharedAction in result.Actions)
            {
                if (sharedAction is ITestStepResult)
                {
                    GetStepResults(steps, sharedAction, iterationResult, iteration, getAttachments, testCase, true);
                }
            }
        }
    }