{"id":5783,"date":"2012-01-01T05:04:52","date_gmt":"2012-01-01T05:04:52","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudioalm\/2012\/01\/01\/how-to-associate-automation-programmatically\/"},"modified":"2019-02-14T18:00:24","modified_gmt":"2019-02-15T02:00:24","slug":"how-to-associate-automation-programmatically","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/devops\/how-to-associate-automation-programmatically\/","title":{"rendered":"How To: Associate automation programmatically"},"content":{"rendered":"<p>In my <a href=\"http:\/\/blogs.msdn.com\/b\/gautamg\/archive\/2011\/12\/30\/how-to-get-the-test-case-associated-with-the-unit-test.aspx\" target=\"_blank\">previous post<\/a>, I mentioned about <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ff942471.aspx\" target=\"_blank\">tcm testcase \/import<\/a> scenario.&#160; The tcm testcase \/import associates the test case artifact with automation (which is unit test or other test type).<\/p>\n<p>Another FAQ related to this scenario is &#8211;<\/p>\n<p><strong>Q:<\/strong> How can I programmatically associate automation (because I want to do some customization of my own)?<\/p>\n<p><strong>A:<\/strong> You can simply use the below code snippet.&#160; (You will need to add the same references as in previous post).<\/p>\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\"><span style=\"color: #008000\">\/\/\/ &lt;summary&gt;<\/span><br \/><span style=\"color: #008000\">\/\/\/ Associates an automation to the test case.<\/span><br \/><span style=\"color: #008000\">\/\/\/ &lt;\/summary&gt;<\/span><br \/><span style=\"color: #008000\">\/\/\/ &lt;param name=&quot;testCase&quot;&gt;The test case artifact to which to associate automation&lt;\/param&gt;<\/span><br \/><span style=\"color: #008000\">\/\/\/ &lt;param name=&quot;automationTestName&quot;&gt;The automation test name. It should be fully<\/span><br \/><span style=\"color: #008000\">\/\/\/ qualified of format &quot;Namespace.ClassName.TestMethodName.&lt;\/param&gt;<\/span><br \/><span style=\"color: #008000\">\/\/\/ &lt;param name=&quot;automationTestType&quot;&gt;The automation test type like &quot;CodedUITest&quot;.&lt;\/param&gt;<\/span><br \/><span style=\"color: #008000\">\/\/\/ &lt;param name=&quot;automationStorageName&quot;&gt;The assembly name containing the above<\/span><br \/><span style=\"color: #008000\">\/\/\/ test method without path like MyTestProject.dll.&lt;\/param&gt;<\/span><br \/><span style=\"color: #0000ff\">private<\/span> <span style=\"color: #0000ff\">void<\/span> AssociateAutomation(ITestCase testCase,<br \/>    <span style=\"color: #0000ff\">string<\/span> automationTestName, <span style=\"color: #0000ff\">string<\/span> automationTestType, <span style=\"color: #0000ff\">string<\/span> automationStorageName)<br \/>{<br \/>    <span style=\"color: #008000\">\/\/ Build automation guid<\/span><br \/>    SHA1CryptoServiceProvider crypto = <span style=\"color: #0000ff\">new<\/span> SHA1CryptoServiceProvider();<br \/>    <span style=\"color: #0000ff\">byte<\/span>[] bytes = <span style=\"color: #0000ff\">new<\/span> <span style=\"color: #0000ff\">byte<\/span>[16];<br \/>    Array.Copy(crypto.ComputeHash(Encoding.Unicode.GetBytes(automationTestName)), bytes, bytes.Length);<br \/>    Guid automationGuid = <span style=\"color: #0000ff\">new<\/span> Guid(bytes);<br \/><br \/>    <span style=\"color: #008000\">\/\/ Create the associated automation.<\/span><br \/>    testCase.Implementation = testCase.Project.CreateTmiTestImplementation(<br \/>            automationTestName, automationTestType,<br \/>            automationStorageName, automationGuid);<br \/><br \/>    <span style=\"color: #008000\">\/\/ Save the test. If you are doing this for lots of test, you can consider<\/span><br \/>    <span style=\"color: #008000\">\/\/ bulk saving too (outside of this method) for performance reason.<\/span><br \/>    testCase.Save();<br \/>}<br \/><\/pre>\n<p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In my previous post, I mentioned about tcm testcase \/import scenario.&#160; 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 &#8211; Q: How can I programmatically associate automation (because I want to do some customization of my own)? [&hellip;]<\/p>\n","protected":false},"author":115,"featured_media":45953,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1,252],"tags":[],"class_list":["post-5783","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-testing"],"acf":[],"blog_post_summary":"<p>In my previous post, I mentioned about tcm testcase \/import scenario.&#160; 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 &#8211; Q: How can I programmatically associate automation (because I want to do some customization of my own)? [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/5783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/users\/115"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/comments?post=5783"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/5783\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media\/45953"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media?parent=5783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/categories?post=5783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/tags?post=5783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}