{"id":5793,"date":"2011-12-30T23:18:22","date_gmt":"2011-12-30T23:18:22","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudioalm\/2011\/12\/30\/how-to-get-the-test-case-associated-with-the-unit-test\/"},"modified":"2022-07-27T02:02:46","modified_gmt":"2022-07-27T10:02:46","slug":"how-to-get-the-test-case-associated-with-the-unit-test","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/devops\/how-to-get-the-test-case-associated-with-the-unit-test\/","title":{"rendered":"How To: Get the Test Case associated with the unit test?"},"content":{"rendered":"<p>For running and managing unit tests using <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb385901.aspx\" target=\"_blank\" rel=\"noopener\">Microsoft Test Manager<\/a> (MTM), the users have to do <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ff942471.aspx\" target=\"_blank\" rel=\"noopener\">tcm testcase \/import<\/a> of the test assembly.\u00a0 This step creates a test case work item corresponding to unit test on the client side.\u00a0 With this, users can do many new scenarios \u2013 most importantly start viewing the history\/trend of the result.<\/p>\n<p>A FAQ that I have heard many times in internal and external forums is around this is &#8211;<\/p>\n<p><strong>Q:<\/strong> How to determine programmatically retrieve the <u>test case artifact<\/u> corresponding to this unit test?<\/p>\n<p><strong>A:<\/strong> Because of various reasons (legacy, loose coupling, extensibility etc), the unit test does not have any direct reference to the test case artifact.\u00a0 However, one can get the corresponding test case artifact easily with few lines of code &#8211;<\/p>\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\"><br \/>        <span style=\"color: #0000ff\">private<\/span> ITestCase GetTestCase()<br \/>        {<br \/>            <span style=\"color: #008000\">\/\/ Get the TFS server and project. If those are constant, then hard-code,<\/span><br \/>            <span style=\"color: #008000\">\/\/ else get it from Environment Variables or config file etc.<\/span><br \/>            <span style=\"color: #0000ff\">string<\/span> serverName = <span style=\"color: #006080\">\"http:\/\/gautamg-tcm1:8080\/tfs\/defaultcollection\"<\/span>;<br \/>            <span style=\"color: #0000ff\">string<\/span> projectName = <span style=\"color: #006080\">\"GautamG.unit.1\"<\/span>;<br \/><br \/>            <span style=\"color: #008000\">\/\/ Get Test Management project.<\/span><br \/>            TfsTeamProjectCollection tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(<span style=\"color: #0000ff\">new<\/span> Uri(serverName));<br \/>            ITestManagementService testService = tfs.GetService&lt;ITestManagementService&gt;();<br \/>            ITestManagementTeamProject project = testService.GetTeamProject(projectName);<br \/><br \/>            <span style=\"color: #008000\">\/\/ Query the test case.<\/span><br \/>            <span style=\"color: #0000ff\">string<\/span> interestedFields = <span style=\"color: #006080\">\"[System.Id], [System.Title]\"<\/span>; <span style=\"color: #008000\">\/\/ and more<\/span><br \/>            <span style=\"color: #0000ff\">string<\/span> testCaseName = TestContext.FullyQualifiedTestClassName + <span style=\"color: #006080\">\".\"<\/span> + TestContext.TestName;<br \/>            <span style=\"color: #0000ff\">string<\/span> storageName = Path.GetFileName(Assembly.GetExecutingAssembly().CodeBase);<br \/>            <span style=\"color: #0000ff\">string<\/span> query = <span style=\"color: #0000ff\">string<\/span>.Format(CultureInfo.InvariantCulture,<br \/>                <span style=\"color: #006080\">\"SELECT {0} FROM WorkItems WHERE Microsoft.VSTS.TCM.AutomatedTestName = '{1}'\"<\/span> +<br \/>                <span style=\"color: #006080\">\"AND Microsoft.VSTS.TCM.AutomatedTestStorage = '{2}'\"<\/span>,<br \/>                interestedFields, testCaseName, storageName);<br \/><br \/>            ITestCase foundTestCase = <span style=\"color: #0000ff\">null<\/span>;<br \/>            IEnumerable&lt;ITestCase&gt; testCases = project.TestCases.Query(query);<br \/>            <span style=\"color: #0000ff\">if<\/span> (testCases.Count() == 1)<br \/>            {<br \/>                foundTestCase = testCases.First();<br \/>            }<br \/><br \/>            <span style=\"color: #0000ff\">return<\/span> foundTestCase;<br \/>        }<br \/><\/pre>\n<p>\n<\/div>\n<p>To compile the above code, you will need reference to following dll (and include namespaces appropriately) &#8211;<\/p>\n<p><img decoding=\"async\" style=\"border-right-width: 0px;padding-left: 0px;padding-right: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px;padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2011\/12\/4062.image_thumb_2F62E044.png\" width=\"424\" height=\"120\" \/><\/p>\n<ul>\n<li>Gautam<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>For running and managing unit tests using Microsoft Test Manager (MTM), the users have to do tcm testcase \/import of the test assembly.\u00a0 This step creates a test case work item corresponding to unit test on the client side.\u00a0 With this, users can do many new scenarios \u2013 most importantly start viewing the history\/trend of [&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-5793","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-testing"],"acf":[],"blog_post_summary":"<p>For running and managing unit tests using Microsoft Test Manager (MTM), the users have to do tcm testcase \/import of the test assembly.\u00a0 This step creates a test case work item corresponding to unit test on the client side.\u00a0 With this, users can do many new scenarios \u2013 most importantly start viewing the history\/trend of [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/5793","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=5793"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/5793\/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=5793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/categories?post=5793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/tags?post=5793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}