Neeraja Reddy from the testing team at Microsoft just provided a pretty cool code snippet to perform a screencapture – programmatically of a failed test:
public void MyTestCleanup()
if (TestContext.CurrentTestOutcome == UnitTestOutcome.Failed)
Image img = UITestControl.Desktop.CaptureImage();
By placing this code in the TestCleanup method, whenever a test fails, the last screen of the test will be captured and saved in the test results directory which will automatically be captured at the end of the test run.