{"id":3370,"date":"2014-03-23T11:24:44","date_gmt":"2014-03-23T11:24:44","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/23\/run-failed-test-at-runtime-with-mstest-vsts2010-collection-of-common-programming-errors\/"},"modified":"2014-03-23T11:24:44","modified_gmt":"2014-03-23T11:24:44","slug":"run-failed-test-at-runtime-with-mstest-vsts2010-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/23\/run-failed-test-at-runtime-with-mstest-vsts2010-collection-of-common-programming-errors\/","title":{"rendered":"Run failed test at runtime with MsTest VSTS2010-Collection of common programming errors"},"content":{"rendered":"<p>Hi Avijit,<\/p>\n<p>1). I think you can use <strong>TestContext.CurrentTestOutcome == UnitTestOutcome.Failed<\/strong> to determine whether a test case is failed or not. Then run these failed tests again. According to this case, you can achieve that with the following code:<br \/>\nif\u00a0(TestContext.CurrentTestOutcome\u00a0==\u00a0UnitTestOutcome.Failed)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<br \/>\n{\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<br \/>\nvar\u00a0type\u00a0=\u00a0Type.GetType(TestContext.FullyQualifiedTestClassName);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<br \/>\nif\u00a0(type\u00a0!=\u00a0null)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<br \/>\n{\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<br \/>\nvar\u00a0instance\u00a0=\u00a0Activator.CreateInstance(type);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<br \/>\nvar\u00a0method\u00a0=\u00a0type.GetMethod(TestContext.TestName);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<br \/>\ntry\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<br \/>\n{<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0method.Invoke(instance,\u00a0null);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<br \/>\n}\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<br \/>\ncatch\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<br \/>\n{\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<br \/>\n}<br \/>\n2). Also, if you already determined that test case is failed again, you can create a screenshot programmatically. Then attach it to the test case. See the following code quoted from this blog:http:\/\/www.mindfiresolutions.com\/How-to-take-screenshot-programmatically-and-mail-it-in-C-647.php<\/p>\n<pre>Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);\nGraphics graphics = Graphics.FromImage(bitmap as Image);\ngraphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);<\/pre>\n<p>\nThanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi Avijit, 1). I think you can use TestContext.CurrentTestOutcome == UnitTestOutcome.Failed to determine whether a test case is failed or not. Then run these failed tests again. According to this case, you can achieve that with the following code: if\u00a0(TestContext.CurrentTestOutcome\u00a0==\u00a0UnitTestOutcome.Failed)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var\u00a0type\u00a0=\u00a0Type.GetType(TestContext.FullyQualifiedTestClassName);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if\u00a0(type\u00a0!=\u00a0null)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var\u00a0instance\u00a0=\u00a0Activator.CreateInstance(type);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var\u00a0method\u00a0=\u00a0type.GetMethod(TestContext.TestName);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 try\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 { \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0method.Invoke(instance,\u00a0null);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 catch\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 } 2). Also, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3370","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3370"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3370\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}