Screenshot on Test Failure

By | 2011-03-24T08:23:08+00:00 March 24th, 2011|Coded UI, Software Testing, Team Foundation Server|2 Comments

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();
        img.Save(Path.Combine(TestContext.TestResultsDirectory, "FailureSnapshot.png"));

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.

About the Author:


  1. […] Screenshot on Test Failure from Jeff Levinson Jeff highlights a code snippet published by a member of the testing team at Microsoft on how to programmatically do a screen capture during an automated test. Really cool stuff that is insanely easy to do. […]

  2. Ashley Lewis November 4, 2014 at 9:37 pm

    Error: An object reference is required for the non-static field, method, or property ‘Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.CurrentTestOutcome.get’

Leave A Comment