Run failed test at runtime with MsTest VSTS2010-Collection of common programming errors
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 (TestContext.CurrentTestOutcome == UnitTestOutcome.Failed)
{
var type = Type.GetType(TestContext.FullyQualifiedTestClassName);
if (type != null)
{
var instance = Activator.CreateInstance(type);
var method = type.GetMethod(TestContext.TestName);
try
{
method.Invoke(instance, null);
}
catch
{
}
2). 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
Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); Graphics graphics = Graphics.FromImage(bitmap as Image); graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
Thanks.