Skip to content

Instantly share code, notes, and snippets.

@jclement
Last active December 18, 2015 01:58
Show Gist options
  • Select an option

  • Save jclement/5707307 to your computer and use it in GitHub Desktop.

Select an option

Save jclement/5707307 to your computer and use it in GitHub Desktop.
When we upgraded to Visual Studio 2012 we ended up with a problem with many of our test cases which included newlines in the action/expected results fields. These steps were stored as a ParameterizedString (Plain Text) in the database and with 2012 they are now stored ParameterizedString (HTML). Unfortunately, the upgrade didn't convert this dat…
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.TestManagement.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System.Net;
namespace MigrationToScrumProcessTemplate
{
class Program
{
static bool isHtml(ParameterizedString v)
{
return v.ToString().ToUpperInvariant().StartsWith("<DIV>");
}
static void Main(string[] args)
{
if (args.Count() != 1)
{
// TFS URL as first parameter
Console.WriteLine("Incorrect number of command line parameters.");
return;
}
var tpc = new TfsTeamProjectCollection(new Uri(args[0]));
ITestManagementTeamProject teamProject = tpc.GetService<ITestManagementService>().GetTeamProject("PROJECT NAME");
var store = tpc.GetService<WorkItemStore>();
var collection = store.Query("SELECT [System.Id] FROM WorkItems ORDER BY [System.Id]");
foreach (WorkItem item in collection)
{
if (item.Type.Name == "Test Case" && item.Project.Name == teamProject.TeamProjectName)
{
ITestBase test = teamProject.TestCases.Find(item.Id);
bool updated = false;
foreach (ITestAction action in test.Actions)
{
if (action is ITestStep)
{
var step = action as ITestStep;
if (!isHtml(step.Title))
{
step.Title = WebUtility.HtmlEncode(step.Title.ToString());
updated = true;
}
if (!isHtml(step.ExpectedResult))
{
step.ExpectedResult = WebUtility.HtmlEncode(step.ExpectedResult.ToString());
updated = true;
}
}
}
if (updated)
{
Console.WriteLine(item.Id + " ==> " + item.Title);
test.Save();
}
}
}
Console.WriteLine("Done!");
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment