1. Team Test is the unit Test framework integrated by Visual Studio test System, which supports:
Code generation for the test method stub (stub).
Run the test in the IDE.
Merges test data that is loaded from the database.
After the test run completes, code coverage analysis is performed.
2. Test stub generation:
A unit test is a test of a method, so the unit of the test is the method;
Right-click on the class or method, "Create Unit Test";
When generating unit test stubs automatically, you can choose to generate unit test code for multiple methods under one class at the same time.
VSTT supports testing of private methods, you may see some proxy classes generated using the reflection mechanism. (different from NUnit).
3, the structure of the test Project organization:
First you need to organize your test code into a separate test project to keep your product code clean. (VS support for specialized Test-class projects);
The generated test project contains references to Microsoft.VisualStudio.QualityTools.UnitTestFramework and to test projects.
It is recommended that each target class correspond to a test class, which is physically corresponding to a. cs file;
4,
Test Code implementation:
Test the structure of the class:
Each target class to be tested generates a corresponding test class with a [TestClass ()] declaration;
For each method of the target class, a method that is declared with [TestMethod ()] in the test class, and the signature of the test method must be an instance method without parameters
A static method identified by [ClassInitialize ()] and [ClassCleanup ()] that represents the initialization code for the test class and the method to execute after the completion of all unit tests in the test class;
An instance method identified by [TestInitialize ()] and [TestCleanup ()] that represents a piece of code to run before each unit test executes;
Team Test uses the reflection mechanism to search all testclassattribute-decorated classes in a test assembly, and then find the methods modified by TestMethodAttribute to determine what to do
Test Assertion Class Assert
The Assert assertion class is the key class used to determine whether a test passes, and it has a variety of powerful test methods that fail if they do not get the expected results.
You can have multiple test assertions in one test.
The error hints in assert assertions should be as accurate and clear as possible.
Use Assert.Inconclusive ("TODO: code to validate the target") to represent some tests that have not yet been fully implemented, at which point the test results behave as a third state different from pass and failed.
Expected exception:
General Method:
[ExpectedException (typeof (ArgumentException),
"A userId of NULL was inappropriately allowed.")]
A more flexible approach:
Try and catch in the test method, then compare the type of catch to the exception and the type of exception you expect, and the error is different:
Assert.isnotnull (Exception,
"The expected exception is not thrown.");
Assert.areequal<type> (
typeof (ArgumentException), exception. GetType (),
"The exception type was unexpected.");