The procedure is brief, and the special department explains it.
Take VS2015 as an example.
1. Create a solution scenario, such as adding a ConsoleApplication1 solution.
2. In the default project for the solution, add a Calc class
3. Set Calc to public and add a static method.
Public class Calc { publicstaticint ADD (intint b) { return A + b; } }
4, on the Add method, right click "Create Unit Test", in the Popup dialog box, select "Test project name" and "Output file", other content selected by default. automatically creates a method.
5, the completion method and the assertion end can be.
namespace consoleapplication1.tests{ [TestClass ()] publicclass calctests { [TestMethod ()] Public void addtest () { intten; int c = Calc.add (A, b); assert.areequal (+, c); }}}
6. At this point, for the method we are testing, there is a summary of the test case health that is automatically added to vs.
7, later modified this method, click Run test cases can.
Assertions include the following, the first parameter is your expected value (Except), the second argument is the return of the function or affects the actual values that the program produces (Actual)
①assert.areequal
The main thing is to verify that the effect of the function or return the value is consistent with the expected, this method is not suitable for validating the returned dataset and the collection of data, mainly for the single type of string, number, and so on, it also has a generic overload, this is better, it is recommended to use, it also has a third parameter, is a string type of message that is basically not used!
Example:assert.areequal<string> ("A", "a", "Cheng Xu Yuan");
②assert.arenotequal
There is nothing to say, contrary to the above, the main is to verify that the actual value is not equal to the expected situation!
③assert.aresame
Determine if the actual value is consistent with the expected value type!
④assert.arenotsame
Contrary to the above, the test type is inconsistent!
⑤assert.isnotnull,assert.isnull,assert.istrue,assert.isfalse
Look at the names of these methods to know what it means!
⑥assert.isinstanceoftype,assert.isnotinstanceoftype
Determines whether the specified object is of the specified type!
⑦assert.fail
Forcing the assertion to fail, regardless of whether the previous assertion succeeds, but the test result is wrong because I forced the assertion to fail!
C # Unit Test small example