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
 

Thursday, August 1, 2013

TFS API: How to get latest test result for a test case

We can retrieve test results for particular test case using TFS API. That's straight forward and there are in build methods. I find people asking for how they can retrieve latest test result for particular test case and I got it solved so thought of sharing here. below code snippet will illustrate how to -

1. Connect to TFS Server/Collection/team project

Uri tfsUri = new Uri("http://servername:8080/tfs/collectionname");
teamProjectCollection = new TfsTeamProjectCollection(tfsUri);
 
iTestManagementService = teamProjectCollection.GetService<ITestManagementService>();
tfsConnectedTeamProject = iTestManagementService.GetTeamProject("Team Project Name");
 
2. Once connected to TFS server, call APIs to get test results for any particular test case. Below line of code will get all results associated with passed test case Id and this returned list is not sorted

var testResults = tfsConnectedTeamProject.TestResults.ByTestId(52737);

3. We can sort this using below code using LastUpdated property of ITestCaseResult and it will return you sorted list of test result.

var resSort = from res in testResults
orderby res.LastUpdated descending
select res;
 
4. Now if you need only latest test result from result collection then below is code. Use above sorted list and retrieve top most record.

var latestTestResult = resSort.First<ITestCaseResult>();

Hope this help you all.

Thanks,
Vivek

Tuesday, July 23, 2013

How to: Add users to list of code reviewer in TFS 2012

With TFS 2012 Update 2, we have new feature where we can enable code review in TFS itself. Here are steps to add users to reviewer list.

1. Open TFS Team Web Access
2. It will open in Overview mode
3. In Right side pane, there are options like ACTIVITIES, MEMBERS, ADMINISTRATION
4. ADMINISTRATION link is for Sprint management.
5. For user management - Click on Top Right Setting symbol [Next to Help link]
6. Select Project and select your team Left side panel. Here you can add users you want to. There is Add  button on top. you can add single user or user(s) group. Also you can add multiple Administrators.
7. Once done re-open VSTS and connect team explorer.
8. Create new review request and you will see added user(s) in reviewer list.

Thanks,

Monday, July 22, 2013

Automated Test Run Error: There is no test with specified id {GUID}

Those who use Microsoft Test Manager to run their automation, find this error common and some time irritating. As the error states, it unable to find specific test case in associated build to test plan. There may be other reason for this such as -

1. Change in class name spaces but didn't updated automation association
2. Build assigned to test plan is not correct or old one
3. Missing test assemblies in assigned build

Resolution:

1. Check if your test assemblies are there in the build drop folder
2. Remove the automation association and re-associate automation method reference.
3. Get the test case ID and make sure it is part of test suite in test plan.
4. Check if associated automated test name is correct. Error states that you are running your test against build which doesn't have associated test in it.
5. Queue new build and once succeeded - assign it to your test plan and make sure this build has all tests which are associated with your automation. 

Thanks,

Wednesday, July 17, 2013

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

Problem Definition:Most TFS or Microsoft Test Manager users find it difficult to update test execution result to MTM. Assume that you are done with 100+ test case execution manually and now you want to upload results (Pass/Fail/Block) for all those test cases and also link bugs, test cases to result as needed. Doing this manually is very time consuming. At this time there is no way to support this ask in MTM versions but yes, we can achieve this by writing our own code using TFS SDK APIs.

Resolution:I came along with this issue some time back and created a tool using TFS APIs which upload results to MTM very smoothly.

Here is how my tool works: 

 1. Run tool exe
 2. Connect to team foundation server you want to
 3.  Select test plan -> Test Suite
 4. Click Get Test Case button
 5. This will create excel with test cases from selected test suite.
 6. There is filter if you want to upload results specifically for manual or automated or all test cases
 7.  Open created excel and update it with results (Pass/Fail/Block). Add comments for block and bug Id for failed test cases. (There is column for this.)
 8. Once ready, click upload button and your marked test case results will be uploaded to Microsoft test Manager
 9. You can view your results from Test tab in MTM


Complete Implementation:

Let's go through step by step code snippet, screen if any. You need to consolidate this code if you want to implement it for you.
1. Connect to team foundation server. This code will show  you connect to.. dialog box so you can choose/add team server and will allow single project selection only.
2. Get Test Plans from selected teal collection and team project above
3. When you select test plan, it will populate drop down with test suites belongs to selected test plan
4. After this, you need to pull out test cases from selected test suite. It will pull out only test cases which are active (Ready to mark results for)
5. Here is code used to create required excel file
6. Step 4-5 will create excel with required columns and test case details. Here is how excel looks - (file will be created at your exe path)
7. Update this excel with result you want to. Don't forget to add bug for failed case and comments for blocked test case. Once done save and close the excel
8. Now last step is to upload result using above updated excel. Here is code will do that. It will create test run for test plan and for selected test cases. Loop through each test case and mark the result and save to results then test run.
8.1 It will first read the excel and then upload the result
8.2 Now upload results to Test Manager using below code :


You can find tool source HERE

And you are done here. I hope this will help you. Please post your comments/feedback to make this better.

Thanks,
Vivek

Wednesday, June 26, 2013

SQL Server Interesting Fact

Just try out this

DECLARE @ AS INT
SET @= 6
PRINT @

What you think by looking code?
Isn't it throw an error....but
that's true internally '@' is treated as variable so it will show you output as normal variable.
Just try it..

Add attachment to Test Case Result using TFS API

We can add attachment to test case result using TFS APIs. Here is small code snippet I have created.

ITestRun testRun = _plan.CreateTestRun(false);
ITestCaseResultCollection results = testRun.QueryResults();
ITestIterationResult iterationResult;
 
foreach (ITestCaseResult result in results)
{
     result.Attachments.Add(new Attachment("FileName", "Comments if any"));
}
 
results.Save(true);
testRun.Save();
testRun.Refresh();

Create Test Case Step Result using TFS API

We can create test case step result using TFS APIs. Here is small code snippet I have created.

ITestRun testRun = _plan.CreateTestRun(false);
ITestCaseResultCollection results = testRun.QueryResults();
ITestIterationResult iterationResult;
 
foreach (ITestCaseResult result in results)
{
    iterationResult = result.CreateIteration(1);   
    foreach (ITestStep testStep in result.GetTestCase().Actions)   
   {      
       ITestStepResult stepResult = iterationResult.CreateStepResult(testStep.Id);      
       stepResult.Outcome = TestOutcome.Passed; //you can assign different states here
       iterationResult.Actions.Add(stepResult);   
   }   
   iterationResult.Outcome = TestOutcome.Passed;   
   result.Iterations.Add(iterationResult);
}
 
results.Save(true);
testRun.Save();
testRun.Refresh();

Making changes to Child Test suite 'State' will affect the Test plan 'State'?

This question confuses many of us who use MTM for test case management. Curious how state of Test Plan , Test Suite works? Making changes to Child Test suite 'State' will affect the Test plan 'State'?

Answer is No. Changes to Child Test suite 'State' won't affect the Test plan 'State'. Test Plan contain one or more different test suites.
State of Test Plan are:
Active: If Test Plan is set to active which mean that you can run test cases from child test suites of test plan.
Inactive: If it is inactive then you cannot run test from its test suites. you can only view, edit test suites.
And yes State of Test Plan do affects its child test suites. 

State of Test Suite are:
In Planning: Test Suite is not ready to run
In Progress: Test Suite is ready for use/execution
Completed: Test Suite execution completed

Test Suite states won't affect Test Plan. Test Suite state should be In Progress to run test from it.
If you delete Test Plan then it will delete all its test suites and test results so its always better to change Test Plan state to Inactive.

Hope this help you.