none
Sample Code to Link CodedUI Test Methods to Test Cases imported in MTM RRS feed

  • Question

  • Can you please provide us the sample code to link codedui test methods to test cases imported in MTM

     Same as above I need code for create link between test case work items to codedui test method.

    Right now we have more than 1000 test case work items in MTM for each feature. So we are finding to manually map each test case work item to coded UI test method

     

    • Moved by Vishnu [MSFT]Microsoft employee Thursday, April 7, 2011 12:35 PM This applies to any test type (From:Visual Studio UI Automation Testing (includes CodedUI))
    Thursday, January 13, 2011 5:06 AM

Answers

  • Hi lxllois,

    I struggled with that error myself for quite some time... The Test GUID you pass into the CreateTMITestImplementation method have to be the correct GUID generated from the name of the Test Method containing the code executing the test. In order to get this GUID you need to run an algorithm to generate it from the Test Method Name. That algorith is posted below (and you owe me a coke for this! hehe)

      /// <summary>
    
      /// Gets the name of the GUID from test method.
    
      /// </summary>
    
      /// <param name="testMethodName">Name of the test method.</param>
    
      /// <returns></returns>
    
      private Guid GetGuidFromTestMethodName(string testMethodName)
    
      {
    
       SHA1Managed crypto = new SHA1Managed();
    
       byte[] bytes = new byte[16];
    
       Array.Copy(crypto.ComputeHash(Encoding.Unicode.GetBytes(testMethodName)), bytes, 16);
    
       return new Guid(bytes);
    
      }
    
    

    For the code above to work, you need to add a reference to "System.Security.Cryptography" to your project and also to the usings section of your class.

    Hope you get it working! :)

    Danie Spreeth


    Monday, May 23, 2011 7:07 AM
  • Thursday, April 7, 2011 2:40 PM

All replies

  • Hi Arjun,

    I am currently facing the same issue you are - even though not with as many testcases.

    I have a Code-Generator creating my Coded UI Tests, and I need to now programatically link the created test methods to the relevant Test Cases.

    Have you found a solution for this yet?

    Hoping for a quick response...

    Danie Spreeth

    Thursday, April 7, 2011 12:25 PM
  • Hi Vishnu,

    I see you have moved this post, yet I believe it was originally in the correct location.

    When MANUALLY linking a Test case to a Test Method inside a CodedUI Test, this HAS TO BE done via Visual Studio, MTM cannot perform this function.

    Thanks

    Danie Spreeth

    Thursday, April 7, 2011 2:16 PM
  • Hi,

    So far I have got the code below. Even though it executes without any errors, afterwards, the TestCases are still NOT linked.

    Please advise URGENTLY.

    int testCaseID = int.Parse(kvp.Value);
    
    string testName = kvp.Key;
    
    WorkItem wi = wis.GetWorkItem(testCaseID);
    
    wi.Fields["Microsoft.VSTS.TCM.AutomatedTestName"].Value = string.Format("{0}.{1}.{2}", m_wrapperNamespace, m_wrapperClassName, testName);
    
    wi.Fields["Microsoft.VSTS.TCM.AutomatedTestStorage"].Value = string.Format("{0}.dll", m_wrapperClassName);
    
    wi.Fields["Microsoft.VSTS.TCM.AutomatedTestType"].Value = "CodedUITest";
    
    wi.Save();

     

     

    Danie Spreeth.

    Thursday, April 7, 2011 2:18 PM
  • Thursday, April 7, 2011 2:40 PM
  • tcm testcase /import should help you if you have 1000s of tests. Please refer http://msdn.microsoft.com/en-us/library/ff942471.aspx

    Thanks,

    Anuj

    Friday, April 8, 2011 2:17 AM
  • Hi Anuj,

     

    Thank you for the reply - but that was not what my issue was. The issue was that I already had 100s of existing Test Cases in MTM and I needed to automatically (or rather programatically) link these existing Test Cases to Test Methods for automation purposes.

    I eventually stumbled upon the page linked to in my previous post (http://social.msdn.microsoft.com/Forums/en/vsmantest/thread/84b7cf10-0109-4494-84a3-a6b42f84468f) and that resulted in the following code.

     

      private void LinkTestMethodsToTestCases()
    
      {
    
       ITestManagementService tms = m_activeTFSCollection.GetService<ITestManagementService>();
    
       ITestManagementTeamProject tmProj = tms.GetTeamProject(m_tfsProjectName);
    
       Guid testGUID = Guid.Parse("{057F74AC-0219-473A-806C-73AA39ADAF7A}");
    
    
    
       foreach (KeyValuePair<string, string> kvp in m_TestMethodsAndTestCaseIDs)
    
       {
    
        int testCaseID = int.Parse(kvp.Value);
    
        string testName = kvp.Key;
    
        string wiQuery = string.Format("SELECT * FROM WorkItems WHERE ID = {0}", testCaseID.ToString());
    
        IEnumerable<ITestCase> testCases = tmProj.TestCases.Query(wiQuery);
    
        foreach (ITestCase tc in testCases)
    
        {
    
         tc.Implementation = tmProj.CreateTmiTestImplementation(
    			string.Format("{0}.{1}.{2}", m_wrapperNamespace, m_wrapperClassName, testName),
    
                       	"CodedUITest", string.Format("{0}.dll", m_wrapperClassName), testGUID);
    
         tc.Save();
    
        }
    
       }
    
      }
    
    

    Maybe this can help someone else struggling with the same...

    Regards,

    Danie Spreeth


    • Proposed as answer by Danie Spreeth Friday, April 8, 2011 5:47 AM
    Friday, April 8, 2011 5:45 AM
  • the same method as you mentioned ,but when I run the test case and  got the error "There is no test with specified Id {b7587acd-026c-5150-ebae-c0c1e992fbb5}." 

    Who know why the error pop up?

    Monday, May 23, 2011 6:46 AM
  • Hi lxllois,

    I struggled with that error myself for quite some time... The Test GUID you pass into the CreateTMITestImplementation method have to be the correct GUID generated from the name of the Test Method containing the code executing the test. In order to get this GUID you need to run an algorithm to generate it from the Test Method Name. That algorith is posted below (and you owe me a coke for this! hehe)

      /// <summary>
    
      /// Gets the name of the GUID from test method.
    
      /// </summary>
    
      /// <param name="testMethodName">Name of the test method.</param>
    
      /// <returns></returns>
    
      private Guid GetGuidFromTestMethodName(string testMethodName)
    
      {
    
       SHA1Managed crypto = new SHA1Managed();
    
       byte[] bytes = new byte[16];
    
       Array.Copy(crypto.ComputeHash(Encoding.Unicode.GetBytes(testMethodName)), bytes, 16);
    
       return new Guid(bytes);
    
      }
    
    

    For the code above to work, you need to add a reference to "System.Security.Cryptography" to your project and also to the usings section of your class.

    Hope you get it working! :)

    Danie Spreeth


    Monday, May 23, 2011 7:07 AM
  • Thank you for your  quick reply. And   the method name  refers to the full name (including the namespace and class name and method name) or only the method name ? Great thanks in advance!

    Monday, May 23, 2011 7:32 AM
  • Yes, you are correct, the code where I call this methods looks like this:

    string fullyQualifiedTestMethodName = string.Format("{0}.{1}.{2}"
    						m_Namespace, 
    						m_ClassName, 
    						m_MethodName);                     
    Guid testMethodGuid = GetGuidFromTestMethodName(fullyQualifiedTestMethodName);

    Regarding the quick replies - you are fortunate to catch me just before my day start getting busy - its early morning in Cape Town, South Africa still :)

    Let me know if you get this working! I struggled for weeks and its good to know I can help someone else get it working quickly!

    • Proposed as answer by lxllois Tuesday, May 24, 2011 3:01 AM
    Monday, May 23, 2011 7:38 AM
  • Great thanks to you:) It works well !Morning:) Now it's afternoon  in Beijing,China .

    Thanks:)

    Monday, May 23, 2011 7:53 AM
  • Glad it works :)

    Have a good afternoon & evening - I still have 7 hours to go before I can rest (after working all weekend as well!!)

    Monday, May 23, 2011 8:01 AM
  • Hi  Danie Spreeth,

    Sorry to trouble you to ask you a question . And I am in trouble to  change  one  Status(Assigned  To) of the test case .When I creat a test case ,it  is assigned to me(default alias),but  in fact the test cases need to be assigned to different  case owner alias. Do you know how to change the alias? Great thanks in advance!

    Tuesday, May 24, 2011 3:12 AM
  • Morning! (or rather afternoon!)

    Changing the assigned to field cannot be that difficult - though I dont have code available to show how to do that. Im a bit busy today too, but I will try and get to your question as soon as possible.

    It is normal for the testcase to be assigned to the person who created it by default though...

    Will have a look at this and let you know asap... Just have another bug to get out of the way first :)

    Danie.

    Tuesday, May 24, 2011 6:00 AM
  • Thanks Danie, your solution using ' GetGuidFromTestMethodName()' worked perfectly for me.

    When using 'CreateTmiTestImplementation()' api to create testcases, our tests were failing with error 'There is no test with specified Id'.  Your suggestion fixed this issue.

    MSDN docs for 'CreateTmiTestImplementation()' make no mention of this, how did you know to do this? I would love to learn.

    Thanks again.  If you're ever in Edmonton, Alberta, please ping me: I owe you a beer for this tip.

    Friday, November 9, 2012 3:27 PM