How To: Associate automation programmatically

In my previous post, I mentioned about tcm testcase /import scenario.  The tcm testcase /import associates the test case artifact with automation (which is unit test or other test type).

Another FAQ related to this scenario is -

Q: How can I programmatically associate automation (because I want to do some customization of my own)?

A: You can simply use the below code snippet.  (You will need to add the same references as in previous post).

 /// <summary>
/// Associates an automation to the test case.
/// </summary>
/// <param name="testCase">The test case artifact to which to associate automation</param>
/// <param name="automationTestName">The automation test name. It should be fully
/// qualified of format "Namespace.ClassName.TestMethodName.</param>
/// <param name="automationTestType">The automation test type like "CodedUITest".</param>
/// <param name="automationStorageName">The assembly name containing the above
/// test method without path like MyTestProject.dll.</param>
private void AssociateAutomation(ITestCase testCase,
    string automationTestName, string automationTestType, string automationStorageName)
{
    // Build 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();
}