Пример #1
0
        /// <summary>
        /// Copies a test case.
        /// </summary>
        /// <param name="sourceTestCase">The source test case.</param>
        /// <param name="destinationTestSuite">The destination test suite.</param>
        private static string CopyTestCase(ITestCase sourceTestCase, IStaticTestSuite destinationTestSuite)
        {
            string results = string.Empty;

            ITestCase destinationTestCase = destinationTestSuite.Project.TestCases.Create();

            destinationTestCase.Title       = sourceTestCase.Title;
            destinationTestCase.Description = sourceTestCase.Description;
            destinationTestCase.Priority    = sourceTestCase.Priority;


            Debug.WriteLine("Test Case: " + sourceTestCase.Title);
            foreach (Field aField in sourceTestCase.CustomFields)
            {
                Debug.WriteLine(string.Format("Field Name: '{0}', Value: '{1}'", aField.Name, aField.Value));
            }

            List <Field> trueCustomFields = (from aField in destinationTestCase.CustomFields.Cast <Field>()
                                             where !aField.ReferenceName.StartsWith("Microsoft") &&
                                             !aField.ReferenceName.StartsWith("System")
                                             select aField).ToList();

            foreach (Field destField in trueCustomFields)
            {
                Field sourceField = (from aField in sourceTestCase.CustomFields.Cast <Field>()
                                     where aField.ReferenceName == destField.ReferenceName
                                     select aField).FirstOrDefault();

                if (sourceField != null)
                {
                    destField.Value = sourceField.Value;
                }
            }

            // Set Area and Iteration Paths
            string areaPath = sourceTestCase.CustomFields["Area Path"].Value.ToString();

            if (areaPath.Contains("\\"))
            {
                areaPath = areaPath.Replace(sourceTestCase.Project.TeamProjectName, destinationTestSuite.Project.TeamProjectName);  // replace the project name

                int areaId = (from node in _destinationAreaNodes
                              where node.Path == areaPath
                              select node.Id).FirstOrDefault();


                destinationTestCase.CustomFields["Area Path"].Value = areaPath;
                destinationTestCase.CustomFields["Area ID"].Value   = areaId;
            }

            string iterationPath = sourceTestCase.CustomFields["Iteration Path"].Value.ToString();

            if (iterationPath.Contains("\\"))
            {
                iterationPath = iterationPath.Replace(sourceTestCase.Project.TeamProjectName, destinationTestSuite.Project.TeamProjectName);  // replace the project name

                int iterationId = (from node in _destinationIterationNodes
                                   where node.Path == iterationPath
                                   select node.Id).FirstOrDefault();

                destinationTestCase.CustomFields["Iteration Path"].Value = iterationPath;
                destinationTestCase.CustomFields["Iteration ID"].Value   = iterationId;
            }

            #region Attachments

            foreach (ITestAttachment sourceAttachment in sourceTestCase.Attachments)
            {
                string filePath = Path.Combine(Path.GetTempPath(), sourceAttachment.Name);

                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }

                sourceAttachment.DownloadToFile(filePath);

                ITestAttachment destinationAttachment = destinationTestCase.CreateAttachment(filePath);
                destinationAttachment.AttachmentType = sourceAttachment.AttachmentType;
                destinationAttachment.Comment        = sourceAttachment.Comment;

                destinationTestCase.Attachments.Add(destinationAttachment);

                destinationTestCase.Save();

                File.Delete(filePath);
            }

            #endregion

            #region Test Steps/Parameters

            foreach (ITestParameter sourceParameter in sourceTestCase.TestParameters)
            {
                destinationTestCase.ReplaceParameter(sourceParameter.Name, sourceParameter.Value);
            }

            foreach (ITestStep sourceAction in sourceTestCase.Actions)
            {
                ITestStep destinationTestStep = destinationTestCase.CreateTestStep();

                destinationTestStep.Title          = sourceAction.Title;
                destinationTestStep.Description    = sourceAction.Description;
                destinationTestStep.TestStepType   = sourceAction.TestStepType;
                destinationTestStep.ExpectedResult = sourceAction.ExpectedResult;
                destinationTestCase.Actions.Add(destinationTestStep);

                // Test Step Attachments
                foreach (ITestAttachment sourceAttachment in sourceAction.Attachments)
                {
                    string filePath = Path.Combine(Path.GetTempPath(), sourceAttachment.Name);

                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }

                    sourceAttachment.DownloadToFile(filePath);

                    ITestAttachment destinationAttachment = destinationTestStep.CreateAttachment(filePath);
                    destinationAttachment.AttachmentType = sourceAttachment.AttachmentType;
                    destinationAttachment.Comment        = sourceAttachment.Comment;

                    destinationTestStep.Attachments.Add(destinationAttachment);

                    try
                    {
                        destinationTestCase.Save();
                    }
                    catch (FileAttachmentException fileException)
                    {
                        destinationTestStep.Attachments.Remove(destinationAttachment);
                        results += string.Format(" - Suite: '{0}', Test Case: '{1}', Could not added attachment '{2}' due to '{3}'" + Environment.NewLine, destinationTestSuite.Title, destinationTestCase.Title, sourceAttachment.Name, fileException.Message);
                    }

                    File.Delete(filePath);
                }
            }

            #endregion

            destinationTestCase.Save();

            destinationTestCase.State = sourceTestCase.State;

            TeamFoundationIdentity sourceIdentity = sourceTestCase.Owner;

            if (sourceIdentity == null)
            {
                results += string.Format(" - Suite: '{0}', Test Case: '{1}', Could not set Assigned To user, not setup as a TFS user." + Environment.NewLine, destinationTestSuite.Title, destinationTestCase.Title);
            }
            else
            {
                TeamFoundationIdentity destinationIdentity = null;
                try
                {
                    destinationIdentity = destinationTestCase.Project.TfsIdentityStore.FindByAccountName(sourceIdentity.UniqueName);
                }
                catch (Exception e) { }

                if (destinationIdentity != null && destinationIdentity.IsActive)
                {
                    destinationTestCase.Owner = destinationIdentity;
                }
                else
                {
                    results += string.Format(" - Suite: '{0}', Test Case: '{1}', Could not set Assigned To user to '{2}'" + Environment.NewLine, destinationTestSuite.Title, destinationTestCase.Title, sourceIdentity.UniqueName);
                }
            }

            destinationTestCase.Save();

            destinationTestSuite.Entries.Add(destinationTestCase);
            destinationTestSuite.Plan.Save();

            return(results);
        }