共用方式為


Unit Test Your Code

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Unit tests give developers and testers a quick way to look for logic errors in the methods of classes in Visual C#, Visual Basic, and Visual C++ projects.

The unit test tools include:

  1. Test Explorer. Test Explorer lets you run unit tests and view their results. Test Explorer can use any unit test framework, including a third-party framework, that has an adapter for the Explorer.

  2. 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.

  3. Microsoft unit test framework for C++. The Microsoft unit test framework for C++ is installed with Visual Studio and provides a framework for testing native code.

  4. Code coverage tools. You can determine the amount of product code that your unit tests exercise from one command in Test Explorer.

  5. 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.

Key tasks

Use the following topics to help with understanding and creating unit tests:

Tasks Associated Topics
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
- Quick Start: Test Driven Development with Test Explorer
- Adding unit tests to existing C++ applications
- Unit testing native code with Test Explorer
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
- Upgrading Unit Tests from Visual Studio 2010
Unit testing managed code: - Writing Unit Tests for the .NET Framework with the Microsoft Unit Test Framework for Managed Code
Unit testing C++ code - Writing Unit tests for C/C++ with the Microsoft Unit Testing Framework for C++
Isolating unit tests - Isolating Code Under Test with Microsoft Fakes
Use code coverage to identify what proportion of your project's code is being tested using unit tests: Learn about the code coverage feature of Visual Studio Application Lifecycle Management testing tools. - Using Code Coverage to Determine How Much Code is being Tested
Perform stress and performance analysis by using load tests for your unit tests: You can create a load test and add your unit tests to it to help isolate performance and stress issues in your application. Note: Creating and using load tests requires Visual Studio Enterprise. - Creating and Editing Load Tests
- How to: Add Web Performance Tests and Unit Tests to a Load Test Scenario
- How to: Remove Web Tests and Unit Tests from a Load Test Scenario
Set and enforce quality gates: You can create quality gates to enforce that tests are run before code is checked in to help ensure the quality of the code. - Set and Enforce Quality Gates
Extend the unit test type: You can add functionality to your tests that might not be in the Unit Test Framework. For example, you can add a test property that specifies if a test should run as a normal user or not. Or you can extend the framework to add row attributes to a method and use the data in that row inside the test. For sample code of how to extend the unit test framework, see the following Microsoft Web site.
Set testing options: For example, you can specify where test results are stored. Configure unit tests by using a .runsettings file

Reviewing Test Results in Microsoft Test Manager

Describes test results and ways to work with them, including how to view, save, and delete them.

Running System Tests Using Microsoft Visual Studio

Provides links to information about using Visual Studio as opposed to using Microsoft Test Manager to run automated tests.

Reference

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.

External resources

Videos

Channel 9: Unit testing your Windows Store apps built using XAML

Forums

Visual Studio Unit Testing

Guidance

Testing for Continuous Delivery with Visual Studio 2012 – Chapter 2: Unit Testing: Testing the Inside

Reference

Content Index for Unit Tests

See Also

Improve Code Quality Testing the application