unit test in VS2015
Frontier
Native Environment: VS2015
Create a project use the. NET version to create a project with the. NET Framework 4.0 1.1
Create a new project with a variable type. I choose to create a console application here
Then add a class Calculate.cs under the current project. The new and added structure is as follows:
Add the following as shown in the Calculate.cs file:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace Example
{
public class Calculate
{
<summary>
The accumulator accumulates starting from 1 to the incoming value
</summary>
<param name= "Ivalue" > Values passed in </param>
<returns> Cumulative Value </returns>
public static int getsum (int ivalue)
{
int sum = 0;
for (int i = 1; I <= ivalue; i++)
{
sum + = i;
}
return sum;
}
}
} 1.2 Create a corresponding unit test
Select the current solution, then right-click to add a new project and select the Unit test project in the test. The name is named by the Test_ project name.
We renamed the default added UnitTest1.cs to: Test_calculate.cs (corresponds to the class name we want to test). Then we need to add a example reference so that the project can be tested.
Right-click on the reference in Test_example, select the example in the project and click OK. In this way we modify the contents of the Test_calculate.cs as follows:
Using System;
Using Microsoft.VisualStudio.TestTools.UnitTesting;
Using Example;
Namespace Test_example
{
[TestClass]
public class Test_calculate
{
[TestMethod]
public void Tesgetsum ()
{
Assert.istrue (Calculate.getsum (10) = = 55);
}
}
}
Then run-> all tests through the test-> to view the test results as follows:
Because 1 to 10 of the cumulative and just equals 55, so the test is correct, if we change 55 to 60, and then look at the effect:
So we can see clearly that there is a problem here in Assert.istrue. 1.3 Common assert usage
Assert.Inconclusive () represents an unauthenticated test;
Assert.AreEqual () tests whether the specified values are equal, and if they are equal, the test passes;
Aresame () is used to verify that the specified two object variable is pointing to the same object or is considered an error
Arenotsame () is used to verify that the specified two object variable is pointing to a different object, otherwise it is considered an error
Assert.istrue () tests whether the specified condition is true and, if true, the test passes;
Assert.isfalse () tests whether the specified condition is false and, if false, 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 not NULL, the test passes; small hint
If you install VS2010 and a later version of VS, you will find that all unit tests are in a pending state when you write the unit test with VS2010 and run. Many people on the Internet have encountered this situation, so individuals suggest using a newer version of VS.