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.
Create a unit test project.
Name your project.
The project is added to your solution.
In the unit test project, add a reference to the project you want to test.
Select the project that contains the code you'll test.
Code your unit test.
You can also create unit test method stubs with the Create Unit Tests command.
Run unit tests
Open Test Explorer.
Run unit tests.
You can see the unit tests that passed or failed 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.
Turn on live unit testing from the Test menu.
View the results of the tests within the code editor window as you write and edit code.
Choose the test result indicators to see more information.
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.
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.
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.
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:
Choose Tools > Extensions and Updates... from the menu bar.
In the Extensions and Updates dialog box, expand the Online category and then Visual Studio Marketplace. Then, choose Tools > Testing.
Select the framework or adapter you want to install, and then choose Download.
Create a class library project, and add it to your solution.
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.
NuGet is an extension of Visual Studio that you can use to add and update libraries and tools for your projects.
In the NuGet Package Manager window, search for and select the plug-in, and then choose Install.
The framework is referenced in your project.
From the class library project's References node, select Add Reference....
In the Reference Manager dialog box, select the project that contains the code you'll test.
Code your unit test.