Run unit tests with your builds

VS 2017 | VS 2015 | VSTS | TFS 2018 | TFS 2017 | TFS 2015

Make sure that your app still works after every check-in and build using Visual Studio Team Services (VSTS) or Team Foundation Server (TFS). Find problems earlier by running tests automatically with each build. When your build is done, review your test results to start resolving the problems that you find.

This example shows how to run unit tests with your build for .NET and ASP.NET apps. It uses the Visual Studio Test task.

Typically you will run unit tests in your build workflow, and functional tests in your release workflow after your app is deployed (usually to a QA environment). Code coverage is available only in the build workflow.

Before you start

Create a build definition

Your build definition must include a test task that runs unit tests. For example, if you're building a Visual Studio solution in VSTS, your build definition should include a Visual Studio Test task. After your build starts, this task automatically runs all the unit tests in your solution - on the same build machine.

  1. If your build definition does not contain a test task, add one to it.

    Add a VS Test task

  2. Edit the Visual Studio Test task. You can add filter criteria to run specific tests, enable code coverage, run tests from other unit test frameworks, and so on.

    Build definition: customize unit test run

    The Visual Studio Test task version 2 supports Test Impact Analysis. For information about all the task settings, see Visual Studio Test task.

    How do I pass parameters to my test code from a build pipeline?

  3. If you also want to test code coverage, set the Code coverage enabled checkbox in the Execution options section.

    Enable code coverage testing

    When tests are run with this option, code coverage information is collected dynamically and assemblies do not need to be instrumented. By default, all assemblies are profiled for collecting coverage information. If you need to exclude specific assemblies and customize code coverage, use a .runsettings file.

    How do I collect and publish code coverage data if I'm not using the Visual Studio Test task?

  4. When you're done, save your build definition.

    Build definition: save

Start the build

  1. Start the build by adding it to the build queue.

    Build definition: queue build

  2. After the build finishes, you can review the test results to resolve any problems that happened. Go to the build to open the build summary.

    Go to Build hub, build definition, build summary

Next step