Collect screenshots, video, logs, and attachments in continuous tests

Azure Pipelines | Azure DevOps Server 2019 | TFS 2018 | TFS 2017 | TFS 2015 | Visual Studio 2017 | Visual Studio 2015

When running tests in a CI/CD pipeline, collecting diagnostic data such as screenshots, video, logs, and attachments is often useful to help troubleshoot failures.

Collect screenshots, logs, and attachments

This example demonstrates how you can collect screenshots when using MSTest. It does not apply if you are using NUnit or XUnit test frameworks.

  1. Add the file you want to include in the test results as a TestResult file.

    Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
    string path = Directory.GetCurrentDirectory() + "SearchTestScreenshot.png";
    this.TestContext.AddResultFile( path); 
  2. Define the TestContext in your test class.

    private TestContext testContextInstance;
    public TestContext TestContext
        return testContextInstance;
        testContextInstance = value;
  3. View the test results and the collected files as attachments to your test results in Azure DevOps or TFS.

    Viewing the collected files

Collect video using the Visual Studio Test task

  1. To collect video, specify the data collector you want to use in a runsettings file. The video data collector captures a screen recording when tests are run.

  2. In the Visual Studio Test task, specify the location of the runsettings file.

    Specifying the location of the runsettings file

Publish attachments when not using the Visual Studio Test task

If you are not using the Visual Studio Test task, you must publish attachments in your test results in a different way:

  • If you are running tests in the build (CI) pipeline, you can use the Copy and Publish Build Artifacts task to publish any additional files created in your tests. These will appear in the Artifacts page of your build summary.

  • Use the REST APIs to publish the necessary attachments. Code samples can be found in this GitHub repository.

The NUnit and JUnit test result formats do not have a formal definition for attachments in the results schema, so the Publish Test Results task cannot publish attachments when these formats are used.

See also

Help and support

Report any problems on Developer Community, get advice on Stack Overflow, and get support via our Support page.