Skip to content

Instantly share code, notes, and snippets.

@bangonkali
Created December 8, 2014 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bangonkali/c3428a1aa15b38a5bf03 to your computer and use it in GitHub Desktop.
Save bangonkali/c3428a1aa15b38a5bf03 to your computer and use it in GitHub Desktop.
Copy Automation Data from One Test Case to Another in MTM Visual Studio 2012
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.TestManagement.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
namespace tfs
{
internal class Program
{
// Variables I want to use globally
private static TfsTeamProjectCollection _tfs;
private static ProjectInfo _selectedTeamProject;
private static string cleanTitle(string title)
{
var options = RegexOptions.None;
var regex = new Regex(@"[ ]{2,}", options);
string titleTemp = regex.Replace(title, @" ");
string[] words = titleTemp.Split('-');
string returnVal = "";
int index = 0;
bool isEnd = false;
foreach (string word in words)
{
if (index > 0 && !isEnd)
returnVal += " - ";
if (word.Contains("[") || word.Contains("]"))
{
// remove all spaces
returnVal += word.Replace(" ", string.Empty);
}
else if (word.Replace(" ", string.Empty).Length > 0)
{
isEnd = true;
returnVal += word.Trim();
}
index++;
}
// Console.WriteLine(returnVal);
return returnVal;
}
private static void Main(string[] args)
{
// The user is allowed to select only one project
var tfsPp = new TeamProjectPicker(TeamProjectPickerMode.SingleProject, false);
tfsPp.ShowDialog();
// The TFS project collection
_tfs = tfsPp.SelectedTeamProjectCollection;
if (tfsPp.SelectedProjects.Count() == 1)
{
// The selected Team Project
_selectedTeamProject = tfsPp.SelectedProjects[0];
var tms = _tfs.GetService<ITestManagementService>();
ITestManagementTeamProject proj = tms.GetTeamProject(_selectedTeamProject.Name);
// proj.TestPlans.Query("Select *");
//
var workItemStore = (WorkItemStore) _tfs.GetService(typeof (WorkItemStore));
// string query = " Where [Work Item Type] = 'Test Case' " +
// " And Contains([Title], '[Chrome]') " +
// " And Contains([Title], '[Setup]') " +
// " And Contains([Title], '[Auto]') ";
// Change the search criteria here. What ever is searched here,
// the automation data will be replaced with what's on the base
// item. The base item is declared later.
string query = " Where [Work Item Type] = 'Test Case'" +
" And [Title] Contains '[Chrome]'" +
" And [Title] Contains 'Security Disabled'" +
"";
string order = " Order By [State] Asc, [Changed Date] Desc ";
string top = " Top 10 ";
IEnumerable<ITestCase> queryResultTestCases =
proj.TestCases.Query("Select * From WorkItems" + query + order);
ITestCase[] testCases = queryResultTestCases.ToArray();
bool enumerateAutomationValue = true;
bool changeAutomationValue = true;
var baseAutomatedTestType = new object();
var baseAutomatedTestId = new object();
var baseAutomatedTestStorage = new object();
var baseAutomatedTestName = new object();
ITestCase[] testcase_automationbase;
if (changeAutomationValue)
{
// This is the base item (test case). What ever automation data is in the base item test case
// it is copied to the test cases found by the search criteria.
testcase_automationbase = proj.TestCases.Query("Select * From WorkItems Where ID=37923").ToArray();
if (testcase_automationbase.Count() != 1)
{
throw new Exception("testcase_automationbase was not found.");
}
baseAutomatedTestType = testcase_automationbase[0].CustomFields["Automated Test Type"].Value;
baseAutomatedTestId = testcase_automationbase[0].CustomFields["Automated Test Id"].Value;
baseAutomatedTestStorage = testcase_automationbase[0].CustomFields["Automated Test Storage"].Value;
baseAutomatedTestName = testcase_automationbase[0].CustomFields["Automated Test Name"].Value;
}
for (int x = 0; x < testCases.Count(); x++)
{
Console.WriteLine("Test Case " + testCases[x].Id + ": " + testCases[x].Title);
// Clean the title if it's dirty
if (testCases[x].Title != cleanTitle(testCases[x].Title))
{
Console.WriteLine("Cleaning test case title: ");
Console.WriteLine("Test Case: " + testCases[x].Title);
Console.WriteLine("New Name: " + cleanTitle(testCases[x].Title));
testCases[x].Title = cleanTitle(testCases[x].Title);
testCases[x].Save();
}
if (enumerateAutomationValue)
{
testCases[x].WorkItem.Open();
testCases[x].CustomFields["Automated Test Type"].Value = baseAutomatedTestType;
testCases[x].CustomFields["Automated Test Id"].Value = baseAutomatedTestId;
testCases[x].CustomFields["Automated Test Storage"].Value = baseAutomatedTestStorage;
testCases[x].CustomFields["Automated Test Name"].Value = baseAutomatedTestName;
testCases[x].WorkItem.Save();
testCases[x].WorkItem.Close();
}
}
}
Console.WriteLine("Prese any key to exit.");
string dfdf = Console.ReadLine();
Console.WriteLine(dfdf);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment