Unit test your code
Unit tests give developers and testers a quick way to look for logic errors in the methods of classes in C#, Visual Basic, and C++ projects.
The unit test tools include:
Test Explorer—You can run unit tests and see their results in Test Explorer. You can use any unit test framework, including a third-party framework, that has an adapter for Test Explorer.
Microsoft unit test framework for managed code—The Microsoft unit test framework for managed code is installed with Visual Studio and provides a framework for testing .NET code.
Microsoft unit test framework for C++—The Microsoft unit test framework for C++ is installed as part of the Desktop development with C++ workload. It provides a framework for testing native code. Google Test, Boost.Test, and CTest frameworks are also included, and third-party adapters are available for additional test frameworks. For more information, see Write unit tests for C/C++.
Code coverage tools—You can determine the amount of product code that your unit tests exercise from one command in Test Explorer.
Microsoft Fakes isolation framework—The Microsoft Fakes isolation framework can create substitute classes and methods for production and system code that create dependencies in the code under test. By implementing the fake delegates for a function, you control the behavior and output of the dependency object.
You can also use IntelliTest to explore your .NET code to generate test data and a suite of unit tests. For every statement in the code, a test input is generated that will execute that statement. A case analysis is performed for every conditional branch in the code.
Use the following topics to help with understanding and creating unit tests:
|Quick starts and walkthroughs: Use the following topics to learn unit testing in Visual Studio from code examples.||- Walkthrough: Creating and running unit tests for managed code
- Quickstart: Test-driven development with Test Explorer
- Add unit tests to existing C++ applications
|Unit testing with Test Explorer: Learn how Test Explorer can help create more productive and efficient unit tests.||- Unit test basics
- Create a unit test project
- Run unit tests with Test Explorer
- Install third-party unit test frameworks
|Unit testing C++ code||- Write unit tests for C/C++ with the Microsoft Unit Testing Framework for C++|
|Isolating unit tests||- Isolate code under test with Microsoft Fakes|
|Use code coverage to identify what proportion of your project's code is tested: Learn about the code coverage feature of Visual Studio testing tools.||- Use code coverage to determine how much code is being tested|
|Perform stress and performance analysis by using load tests: You can create a load test and add your unit tests to it to help isolate performance and stress issues in your application.||- Load testing (Azure Test Plans and TFS)|
|Set quality gates: You can create quality gates to enforce that tests are run before code is checked in or merged, to help ensure the quality of the code.||- Check-in policies (Azure Repos TFVC)|
|Set testing options: For example, you can specify where test results are stored.||Configure unit tests by using a .runsettings file|
API reference documentation
- Microsoft.VisualStudio.TestTools.UnitTesting describes the UnitTesting namespace, which provides attributes, exceptions, asserts, and other classes that support unit testing.
- Microsoft.VisualStudio.TestTools.UnitTesting.Web describes the UnitTesting.Web namespace, which extends the UnitTesting namespace by providing support for ASP.NET and web service unit tests.
We'd love to hear your thoughts. Choose the type you'd like to provide:
Our feedback system is built on GitHub Issues. Read more on our blog.