Migrate Test Cases from TFS to VSTS along with par

2019-03-22 13:06发布

问题:

We are planning to move test cases, build definition and code from TFS to VSTS. But it seems that we are not able to move the parameters and attachments in test case present in MTM to VSTS. Is there a way we can get this done?

回答1:

It's difficult to migrate Test Cases separately from TFS to VSTS with parameters and attachments. See Bulk Migrate Work Item Comments, Links and Attachments

Test Cases

Test plans/suites/cases turned out to be particularly difficult to migrate. We couldn’t find any tooling that would be able to do this for us. It’s possible to manage tests using the TFS SDK on-premise, and there is a restful API for VSTS, so a custom solution could be produced where time is permitting.

Unfortunately, Excel was out of the question, as test plans and suites cannot be queried as usual and test cases when published were not possible to be linked to a test suite or plan. Source Here

However, you can have a try with the VSTS Sync Migrator tool by following the steps mentioned in this article : TFS 2017 Migration To VSTS with VSTS Sync Migrator

You can also try this tool: OpsHub Visual Studio Online Migration Utility

If you want to migrate entire project from TFS to VSTS, then you can use the the official TfsMigrator tool and following the guide to do that.

Note: You need to upgrade your TFS to the supported version first.

You can also referenc below articles for the migration:

  • Migrate TFS to VSTS - Migrate your TFS instance to a VSTS account without any data/history loss (high fidelity)
  • How to migrate MTM Test Cases from TFS 2013 to VSTS?


回答2:

We were able to move test cases from TFS to VSTS(But parameters and attachments of the test cases didn't migrate with them)

Thanks to @Andy Li-MSFT(for helpful links and explanations) and exploring web/brainstorming, we finally wrote a piece of code in C# to copy parameters and attachments from old test cases to newly moved test cases in VSTS.

Namespace used

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.TestManagement.Client;
using System.Net;


 List<int> TestCaseIds_Old = new List<int> { 100, 102, 103 };
            List<int> TestCaseIds_New = new List<int> { 10023, 42102, 67103 };

            TfsTeamProjectCollection teamProjectCollection_Old = new TfsTeamProjectCollection(new Uri("OLD TFS Collection URL"));
            TfsTeamProjectCollection teamProjectCollection_New = new TfsTeamProjectCollection(new Uri("NEW TFS Collection URL"));

            ITestManagementService testManService_Old = teamProjectCollection_Old.GetService<ITestManagementService>();
            ITestManagementService testManService_New = teamProjectCollection_New.GetService<ITestManagementService>();

            var project_Old = testManService_Old.GetTeamProject("OLD Project");
            var project_New = testManService_New.GetTeamProject("NEW Project");

            for (int id = 0; id < TestCaseIds_New.Count; id++)
            {
                var testCase_Old = project_Old.TestCases.Find(TestCaseIds_Old[id]);
                var testCase_New = project_New.TestCases.Find(TestCaseIds_New[id]);
                for (int i = 0; i < testCase_Old.Data.Tables[0].Rows.Count; i++)
                {
                    var rowCollection = testCase_Old.Data.Tables[0].Rows[i].ItemArray;
                    testCase_New.Data.Tables[0].Rows.Add(rowCollection);
                }

                List<string> fileLocations = new List<string>();
                if (testCase_Old.Attachments.Count > 0)
                {
                    WorkItemStore workItemStore = teamProjectCollection_Old.GetService<WorkItemStore>();
                    WorkItem requiredWorkItem = workItemStore.GetWorkItem(TestCaseIds_Old[id]);
                    WebClient webClient = new WebClient();
                    webClient.Credentials = System.Net.CredentialCache.DefaultCredentials;
                    for (int i = 0; i < requiredWorkItem.Attachments.Count; i++)
                    {
                        webClient.DownloadFile(requiredWorkItem.Attachments[i].Uri, "D:\\Attachments\\" + requiredWorkItem.Attachments[i].Name);
                        testCase_New.Attachments.Add(testCase_New.CreateAttachment("D:\\Attachments\\" + requiredWorkItem.Attachments[i].Name, SourceFileAction.None));
                        fileLocations.Add("D:\\Attachments\\" + requiredWorkItem.Attachments[i].Name);
                    }

                }
                testCase_New.Save();
                for (int i = 0; i < fileLocations.Count; i++)
                {
                    File.Delete(fileLocations[0]);
                }

            }