Tuesday, September 17, 2013

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

Wednesday, September 11, 2013

TFS API: Get Work Item History Revisions for parituclar Field

Hello,

I observed many of us struggling to view history for any work item. History is visible but it is hard to visualize changes made like who made changes, to which field and when etc. For this matter, one has to scroll through entire history tab and find it tedious job.

I was thinking what if we could view work item history for selected field with other details like who changed it and when.

Here is solution - I have created a tool which show work item history for selected field. Also shows how may times selected field changed. You can view history by selecting single work item and also export entire work item query history to excel. I find this tool useful so thought of sharing with others.

How it works -

1. Run EXE
2. Prompt you select/add team project server details
3. Populates your My Queries to Treeview
4. Double Click query from tree and it will show work items in right grid
5. Select Work item field from top drop down for which you want to see history
6. Double click any grid cell will show history of selected field
7. Tool will show only result for when selected field was changed. It compares field value with when work item was initially created and if that initial value changed by someone then it will shown in history.
8. Refresh button will refresh query tree in case you made changes to queries. No need to re-run tool
9. Export to excel will let you export history of all work items as per selected query.

Tool GUI -



You can find executable HERE.

Hope this helps you too. Post your comment/feedback to make this better.

Thanks,
Vivek