Get started with unit testing

Use Visual Studio to define and run your unit tests to maintain code health, ensure code coverage, and to find errors and faults before your customers do.

Create unit tests

Create unit tests and run them frequently to make sure your code is working properly.

  1. Create a unit test project.

    Add a unit test project to your solution

  2. Name your project.

    Unit test project template

    The project is added to your solution.

    Unit test project in Solution Explorer

  3. In the unit test project, add a reference to the project you want to test.

    Add a reference to your unit test project

  4. Select the project that contains the code you'll test.

    Select the reference to add

  5. Code your unit test.

    Add code to your unit test

You can also create unit test method stubs with the Create Unit Tests command.

Using the Create unit tests command

Run unit tests

  1. Open Test Explorer.

    On the Test menu, open Test Explorer

  2. Run unit tests.

    Run unit tests in Test Explorer

    You can see the unit tests that passed or failed in Test Explorer.

    Review unit test results in Test Explorer

View live unit test results

If you are using the MSTest, xUnit, or NUnit testing framework in Visual Studio 2017 or later, you can see live results of your unit tests.

  1. Turn on live unit testing from the Test menu.

    Turn on live unit testing

  2. View the results of the tests within the code editor window as you write and edit code.

    View the results of the tests

  3. Choose the test result indicators to see more information.

    Choose the test result indicators

For more details, see Live unit testing.

Generate unit tests with IntelliTest

When you run IntelliTest, you can easily see which tests are failing and add any necessary code to fix them. You can select which of the generated tests to save into a test project to provide a regression suite. As you change your code, rerun IntelliTest to keep the generated tests in sync with your code changes. To learn how, see Generating unit tests for your code with IntelliTest.

Generating unit tests with IntelliTest

Run unit tests with Test Explorer

Use Test Explorer to run unit tests from Visual Studio or third-party unit test projects, group tests into categories, filter the test list, and create, save, and run playlists of tests. You can also debug tests and analyze test performance and code coverage. To learn how, see Run unit tests with Test Explorer.

Running unit tests with Test Explorer

Use code coverage to determine how much code is being tested

To determine what proportion of your project's code is actually being tested by coded tests such as unit tests, you can use the code coverage feature of Visual Studio. To guard effectively against bugs, your tests should exercise or 'cover' a large proportion of your code. To learn how, see Use Code Coverage to Determine How Much Code is being Tested.

Using code coverage to determine how much code is being tested

Use a different unit test framework

You can run unit tests in Visual Studio by using third-party test frameworks such as Boost, Google, and nUnit. Use the plug-in for the framework so that Visual Studio's test runner can work with that framework.

Following are the steps to enable third-part test frameworks:

  1. Choose Tools > Extensions and Updates... from the menu bar.

  2. In the Extensions and Updates dialog box, expand the Online category and then Visual Studio Marketplace. Then, choose Tools > Testing.

    Visual Studio Marketplace

  3. Select the framework or adapter you want to install, and then choose Download.

  4. Create a class library project, and add it to your solution.

    Name the class library project and add it

  5. Install the plug-in. In Solution Explorer, select the class library project, and then choose Manage NuGet Packages... from its right-click or context menu.

    Manage NuGet packages to install the plug-in

    NuGet is an extension of Visual Studio that you can use to add and update libraries and tools for your projects.

  6. In the NuGet Package Manager window, search for and select the plug-in, and then choose Install.

    Install your 3rd-party framework

    The framework is referenced in your project.

    The reference for the 3rd-party unit test framework is added into your solution

  7. From the class library project's References node, select Add Reference....

    Add a reference to the project

  8. In the Reference Manager dialog box, select the project that contains the code you'll test.

    Select the code project for you to test

  9. Code your unit test.

    Add code to your unit test

See also