WrittenHelloworld used by nunitI don't know what to write, as if nunit is like this. however, we sometimes get mad at testing. In fact, nunit is just a tool. A good design principle should be followed for written unit test cases, such as atrip. I can't explain it more here.
The test of the private method is not the nunit function, but passed. net reflection mechanism executes the private method to get the result, and then compare the expectations. so the focus of this talk is. net reflection. although it is just a bit of a bit, readers should be able to understand reflection.
First define a classCalculator, There is a methodAddTo implement simple addition. This is our tested class.
Next, we will test this class:
1 // Tested class to implement simple addition
2 Using System;
3
4 Namespace Privatemethodtest
5 {
6 Public Class Calculator
7 {
8 Private Int Add ( Int A, Int B)
9 {
10ReturnA+B;
11}
12 }
13 }
1 Using System;
2 Using System. reflection;
3 Using Nunit. Framework;
4
5 Namespace Privatemethodtest
6 {
7 [Testfixture]
8 Public Class Calculatortest
9 {
10 // Test add.
11 [Test]
12 Public Void Testadd ()
13 {
14 Int Expected = 3 ;
15
16 Calculator Cal = New Calculator ();
17 Object Result = Invokepmethod ( Typeof (Privatemethodtest. calculator ), " Add " , Cal, New Object [ 2 ] {1,2} );
18
19 Assert. areequal (expected, result );
20 }
21
22 // Auxiliary method: Call Private methods of other classes
23 // Instanceclass: instance of the class, Params: parameter instance of the Method
24 Private Object Invokepmethod (system. Type type, String Methodname, Object Instanceclass, Object [] Params)
25 {
26 // Discover method attributes and provide access to method metadata (from: msdn)
27 // Here, the property of the method refers to the static, virtual, final modification of the method, the parameters of the method, the return value of the method, and other details.
28 // The most important thing is that you can call a method (invoke) through methodinfo)
29 Methodinfo method;
30
31 // Indicates the type of the member to be searched. nonpublic indicates that a non-public member is searched, and instance indicates that an instance Member is searched (not static)
32 // Therefore, the following statement indicates that the search type is not a public instance Member.
33 Bindingflags flags = Bindingflags. nonpublic | Bindingflags. instance;
34
35 // Type is the root of the system. reflection function and is also the main way to access metadata. (From: msdn)
36 // Use the type member to obtain information about the type declaration, such as constructors, methods, fields, and properties)
37 // And class events, as well as the modules and assembly where the class is deployed. (From: msdn)
38 // Type is the source of reflection in. net, just like the class in Java. If no class is connected, you can call the method and get the attribute.
39 // Getmethod: Get methodinfo through method name and search
40 Method = Type. getmethod (methodname, flags );
41
42 // Call the private method: the parameters are the class instance and method parameters respectively.
43 Object Result = Method. Invoke (instanceclass, Params );
44
45 Return Result;
46 }
47 }
48 }
The focus is to call the private method through reflection to obtain the running result. There are no other differences with the public method. Steps for Using Reflection:
1. Create an instance, Calculator Cal = new calculator ()
2. Obtain the type, that is, typeof (Privatemethodtest. Calculator),Namespace + class name
3. Obtain the method, that is, type. getmethod (methodname, flags)
4. Call method (invoke), that is, object result = method. Invoke (Instanceclass,Params);
Therefore, the value of result is to call the add method in Cal, and two parameters are passed in.1 and2The result is compared with expected result 3. The test is successful.