To create a unit test in vs2008
One, open vs2008, create a class library Vstsdemo.
Because I am in the study of the use of vs2008, other versions of VS, the method should be similar, we study it:)
Delete the default generated class and create a Logoninfo class, and then we write a very simple way to get the maximum value in this class Getmax (int[] arynum). The code for the method is as follows:
Namespace Vstsdemo
{
public class Logoninfo
{
public int Getmax (int[] arynum)
{
if (Arynum = = NULL | | arynum.length = = 0)
{
return-1;
}
if (arynum.length = = 1)
{
return arynum[0];
}
int n = arynum[0];
for (int i = 1; i < arynum.length; i++)
& nbsp; {
if (Arynum[i] > N)
{
n = arynum[i];
}
}
return n;
}
}
}
This method does not explain, very simple, it should be understood at a glance. Its function is to find the largest value from the Arynum array.
Second, create a unit test project
Move the mouse over the Getmax class name, right-click, select Options, create unit tests such as:
This will bring up a "Create Unit Test" action box, we directly click OK, and then enter the name of the new project Logoninfotest, such as:
When you are done, VS automatically generates a Logoninfotest test project with a Solution Items folder, and automatically generates a LogonInfoTest.cs file under the Logoninfotest project, such as:
Note: The assert.inconclusive ("Verify correctness of this test method") in the above code is I commented out manually. This line is generated by default, but it's useless. If you do not want to generate by default, you can click Settings when you create a unit test, and then tick out the option to generate this line of code by default.
Third, start the test method Logoninfo
Before we start the test, we need to rewrite the Getmaxtest () method
//<summary>
///getmax test
///</summary>
[TestMethod ()]
public void getmaxtest ()
{
logoninfo target = new Logoninfo ();
int[] arynum = new int[] {1, 3, 4};//This assigns an initial value for arynum
int expected = 4;// Here the setting is the expected value of the Getmax method, from the initial value above and the original intent of the Getmax method we know that the expected value should be 4
int actual;
actual = target. Getmax (Arynum);
assert.areequal (expected, actual);//This determines whether expected and actual are equal, if equal, the test succeeds, and vice versa
//assert.inconclusive ("Verify the correctness of this test method. ");
}
Note assert.areequal (expected, actual) this line of code, Assert.areequa is the test expected and actual is not equal, equality is considered to be successful, unequal is considered a failure. The same methods are used to determine the following: the
Assert.AreEqual () tests whether the specified values are equal, and if they are equal, the test passes;
Assert.Inconclusive () represents an unauthenticated test;
Assert.istrue () tests if the specified condition is true, if true, the test passes;
Assert.isfalse () tests whether the specified condition is false, or false if the test passes;
Assert.isnull () tests whether the specified object is a null reference, and if it is empty, the test passes;
Assert.isnotnull () tests whether the specified object is non-null, and if it is not empty, the test passes;
After the code rewrite is complete, we open Test view, "Testing", "Windows" on the VS menu, and the Test View window appears, such as:
In the Test View window, all the test methods that we created are listed, the mouse moves to the Getmaxtest method, the "Run Selection" is selected in the right-click, and the test begins. The results of the test are displayed in the test Results window, such as:
From the above results we can know whether the method test has passed. Then we can also change the initial value in the test method Getmaxtest () and the expected value, and run the test method again to view the test results.
Code Coverage
Code coverage refers to the extent of the code that is executed when the case runs when the test case is run. The higher the code coverage, the better the case. Each executable statement in the code under test is executed, and the result is more stable. For example, in unit tests, code coverage must be 80% or 90%, so we need to write test cases well.
So how do you see the coverage of your code? We double-click the folder solution items under the Localtestrun.testrunconfig file. In the pop-up window, set the method to view code coverage, such as:
After running the test method, we open the Code Coverage Results window, "testing", "Windows" on the VS menu, and double-click the method in the window where you want to view code coverage, such as:
The results of coverage are as follows:
Where the orange code indicates a partial execution, the red code indicates no execution, and the blue code indicates that all has been performed. Of course, this color can be customized in tools, options, fonts and colors.
This article comes from:. NET Learning Network http://www.lmwlove.com/ac/ID858
ASP. NET unit Test and view Code coverage detailed example