Поделиться через


Unit Tests Overview

Unit tests are programmatic tests written in Visual C# or Visual Basic, or in Visual C++ and compiled with /clr:safe.

Note

For details on how to use unit tests with C++ production code and how to use unit tests written in C++, see Unit Tests and C++.

Unit tests are used to exercise other source code by directly calling the methods of a class, passing appropriate parameters, and then, if you include Assert statements, they can test the values that are produced against expected values. Unit test methods reside in test classes, which are stored in source code files.

You can create unit tests by using a code generation feature that creates the initial source code of the test, or you can write the test completely by hand. Either way, the test class and all test methods are identified by using programmatic attributes.

Each test class is marked with the [TestClass()] attribute. Each unit test is a test method that is marked with the [TestMethod()] attribute. These attributes are assigned automatically when unit tests are generated; when you code unit tests by hand, you have to add class and method attributes yourself. For more information, see Structure of Unit Tests.

Generating Unit Tests

You can generate unit tests by using a dialog box reached through Visual Studio tools windows. Each unit test is created as a C#, Visual Basic, or Visual C++ method. Its code and attributes reside in a source file in a test project of the same language. The new test targets the specific code you want tested. You can generate a single unit test for an individual method, multiple tests for methods that you select, or multiple tests for all the methods in a class or namespace.

The code of a newly generated test compiles, but before it can provide useful test results, you must edit it. For example, you might edit it to make variable assignments and to customize Assert statements. Generated unit tests contains TODO statements that indicate which lines of code must be edited.

Note

The Team System testing tools use naming conventions when it generates unit tests. For example, a test file is named by concatenating the word "Test" with the name of the file that contains the code you are testing; for example, "Class1Test.cs." Names of test classes and test methods are generated by using defaults also. You can change these defaults in the Test Generation Configuration dialog box, which you can open by clicking Configure on the Unit Test Generation dialog box.

For more information, see How to: Generate a Unit Test.

Authoring Unit Tests

You can also author a unit test by hand, without using the generation feature. The best way to do this is to add a unit test to a test project; this creates a stubbed test method that contains no code, but has the [TestMethod()] attribute applied. To complete the test, you must then edit the test in its source file, which in turn resides in a test project in your solution. For more information, see How to: Author a Unit Test and Structure of Unit Tests.

The Unit Testing Framework

The Unit Testing Framework provides many additional Assert classes and other classes that give you flexibility in writing unit tests. For more information, see the documentation on the namespace and types of the Unit Testing Framework under Microsoft.VisualStudio.TestTools.UnitTesting.

Testing Private Methods

Using unit tests, you can test not only public methods but private methods as well. As with public methods, unit tests for private methods are created automatically when you generate tests from the code you want to test. For more information, see How to: Test a Private Method and How to: Regenerate Private Accessors.

Special Kinds of Unit Tests

Additional kinds of unit tests are data-driven unit tests, ASP.NET unit tests, and Web service unit tests. A data-driven test is a unit test that you configure to be called repeatedly for each row of a data source. The data from each row is available to each successive run of the test as input data. For more information, see Overview of Data-Driven Unit Tests. ASP.NET unit tests are used to exercise code in an ASP.NET application as it responds to page requests. ASP.NET unit tests are run inside the ASP.NET application being tested. For more information, see Overview of ASP.NET Unit Tests. For information about Web service unit tests, see Testing Web Services.

See Also

Concepts

Structure of Unit Tests
Unit Tests and C++

Other Resources

Working with ASP.NET Unit Tests
Working with Data-Driven Unit Tests
Managing Tests
Running Tests
Test Results and Analysis