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. Or you can use a different unit test framework to create tests for different code languages.

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 above, you can see live results of your unit tests within the Visual Studio UI.

  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.

    Point to and click on the test result indicators

  3. Point to and click on the test result indicators to see more information.

    View the results of the tests

For more details, see Live Unit Testing in Visual Studio.

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

Q & A

Q: Can I run unit tests in Visual Studio if I use a different unit test framework?

A: Yes, use the plug-in for that framework so that Visual Studio's test runner can work with that framework. Here are some of the unit testing framework plug-ins for Visual Studio.

  1. Use Visual Studio's extension manager to download your plug-in.

    Select 3rd-party unit test plug-ins with extension manager

  2. Download your plug-in from the Visual Studio Gallery under Tools/Testing, or search for it if you know the name.

    Download your plug-in

  3. Create a class library project.

    Create a class library project

    Add the project to your solution.

    Name the class library project and add it

  4. In the class library project, run NuGet to install the plug-in.

    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.

  5. Install your plug-in. If you know the name, you can search for it online.

    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

  6. In the class library project, add a reference to the project you want to test.

    Add a reference to the project

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

    Select the code project for you to test

  8. Code your unit test.

    Add code to your unit test

See also