Associate Test Case Automation using TFS API

Hello,

I have been observing most of users reported that associating automation to test case one by one from Visual Studio Team Explorer is tedious job rather time consuming. I was thinking what if we could achieve this using TFS object model. My idea was fetch test case one by one and associate automation to test case and save test case work item.

Resolution:
Here is code which can use to associate automation in bulk - 

Add below references - 
Microsoft.TeamFoundation
Microsoft.TeamFoundation.Client 
Microsoft.TeamFoundation.WorkItemTracking.Client
Microsoft.TeamFoundation.WorkItemTracking
teamprojectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(collectionUri);

workItemStore = projectCollection.GetService<WorkItemStore>();

//You can either specify work item query to fetch test cases
WorkItemCollection workItemCollection = workItemStore.Query(
     " SELECT * FROM WorkItems " +
     " WHERE [System.TeamProject] = '" + teamProject.Name;

//Or
//Pass test case Id to get test case
WorkItem workItem = workItemStore.GetWorkItem(555);

//Parameter details -
//ITestCase testCase = The test case artifact to which to associate automation
//String automationTestName = The automation test name. It should be fully qualified of format "Namespace.ClassName.TestMethodName
//String automationTestType = The automation test type like "CodedUITest"
//String automationStorageName = The assembly name containing the above est method without path like MyTestProject.dll.

//Get automation guid
    SHA1CryptoServiceProvider crypto = new SHA1CryptoServiceProvider();
    byte[] bytes = new byte[16];
    Array.Copy(crypto.ComputeHash(Encoding.Unicode.GetBytes(automationTestName)), bytes, bytes.Length);
    Guid automationGuid = new Guid(bytes);

// Create the associated automation.
   testCase.Implementation = testCase.Project.CreateTmiTestImplementation(
            automationTestName, automationTestType,
            automationStorageName, automationGuid);

    // Save the test. If you are doing this for lots of test, you can consider
    // bulk saving too (outside of this method) for performance reason.
    testCase.Save();



This is just sample code but I am planning to create small tool and will share here soon.


Thanks,
Vivek

Comments

  1. Hi Vivek Bansod,

    How I catch the testCase?

    Thank's

    ReplyDelete
  2. Hi,
    You can use below line of code to get the test case object -
    WorkItemStore wis = teamProjectCollection.GetService();
    ITestManagementTeamProject project = teamProjectCollection.GetService().GetTeamProject(prInfo.Name);
    ITestCase testcase = project.TestCases.Find();

    then call above associate automation method by passing this test case object. Hope this helps you.

    ReplyDelete
  3. Hi Vivek,

    May I know for the generation of the Automation Guid, does it has to strictly follows the sample code that you provided or I could just do a Guid.NewGuid()?

    I previously asked in the forum, and they told me I can't simply generate the automation id
    http://social.msdn.microsoft.com/Forums/vstudio/en-US/d5df2b42-660d-4281-8d00-68f04bbf7134/associating-automation-via-excel-query-and-publish-can-automated-test-id-be-generated-using?forum=tfsgeneral#ce278509-ecab-41ef-98a4-4f9d87fa7056

    ReplyDelete
  4. Hi again,

    I just tried out the code

    SHA1CryptoServiceProvider crypto = new SHA1CryptoServiceProvider();
    byte[] bytes = new byte[16];
    Array.Copy(crypto.ComputeHash(Encoding.Unicode.GetBytes(automationTestName)), bytes, bytes.Length);
    Guid automationGuid = new Guid(bytes);

    and it seems to be the same as the one generated by TFS :)

    Just curious, how did you know it was using that algorithm?

    ReplyDelete
  5. Hi, Thanks for feedback,

    Actually I was facing similar issue then I tried above code at my end and it just worked fine like real. So thought of sharing with all through my blog. My understanding is, TFS uses same algorithm and hence same results. I found this snippet at MSDN ALM blogs.

    Glad it helped you.

    ReplyDelete

Post a Comment

Popular posts from this blog

How To: Bulk upload Test Case results to Microsoft Test Manager

TFS API: Get Work Item History Revisions for parituclar Field

Add attachment to Test Case Result using TFS API