使用 nUnit 测试 Private 和 Protected 方法

原文:http://www.codeproject.com/csharp/TestNonPublicMembers.asp


Testing Protected Methods

要测试一个 protected 方法,我们的测试类需要继承包含这个 protected 方法的父类,然后在测试类中就可以公开使用这个 protected 方法了,示例如下:

假设要测试下面 ClassLibrary1.Class1 中的 MyProtectedMethod() 方法:

None.gif using  System;
None.gif
None.gif
namespace  ClassLibrary1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for Class1.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Class1
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
protected int MyProtectedMethod(int val1, int val2)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return val1 + val2;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }
 // end of class
InBlock.gif

ExpandedBlockEnd.gif}
  //  end of namespace

下面是测试类代码:
None.gif using  System;
None.gif
None.gif
using  NUnit.Framework;
None.gif
None.gif
namespace  ClassLibrary1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for Tester.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [TestFixture]
InBlock.gif    
public class Tester : Class1
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        [Test]
InBlock.gif        
public void MyProtectedMethod_Test()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Assert.AreEqual(
5base.MyProtectedMethod(23));
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }
 // end of class
InBlock.gif

ExpandedBlockEnd.gif}
  //  end of namespace


Testing Private Methods

测试 private 方法需要使用反射

假设要测试下面 ClassLibrary1.Class1 中的 MyPrivateMethod() 方法:
None.gif using  System;
None.gif
None.gif
namespace  ClassLibrary1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for Class1.
ExpandedSubBlockEnd.gif    
/// </summary>
InBlock.gif    public class Class1
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
protected int MyPrivateMethod(int val1, int val2)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return val1 + val2;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }
 // end of class
InBlock.gif

ExpandedBlockEnd.gif}
  //  end of namespace

下面是测试类代码:
None.gif using  System;
None.gif
using  System.Reflection;
None.gif
None.gif
using  NUnit.Framework;
None.gif
None.gif
namespace  ClassLibrary1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for Tester.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [TestFixture]
InBlock.gif    
public class Tester
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        [Test]
InBlock.gif        
public void MyPrivateMethod_Test()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ClassLibrary1.Class1 class1 
= new ClassLibrary1.Class1();
ExpandedSubBlockStart.gifContractedSubBlock.gif            
object[] aobjParams = new object[] dot.gif34 };
InBlock.gif            
object strRet;
InBlock.gif            strRet 
= RunInstanceMethod( typeof(ClassLibrary1.Class1),
InBlock.gif                
"MyPrivateMethod",
InBlock.gif                class1,
InBlock.gif                aobjParams
InBlock.gif            );
InBlock.gif            Assert.AreEqual(
7, strRet.ToString());
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 运行静态方法
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="t"></param>
InBlock.gif        
/// <param name="strMethod"></param>
InBlock.gif        
/// <param name="aobjParams"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static object RunStaticMethod(System.Type t, string strMethod, 
InBlock.gif            
object [] aobjParams) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            BindingFlags eFlags 
= 
InBlock.gif                BindingFlags.Static 
| BindingFlags.Public | 
InBlock.gif                BindingFlags.NonPublic;
InBlock.gif            
return RunMethod(t, strMethod, 
InBlock.gif                
null, aobjParams, eFlags);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 运行实例方法
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="t"></param>
InBlock.gif        
/// <param name="strMethod"></param>
InBlock.gif        
/// <param name="objInstance"></param>
InBlock.gif        
/// <param name="aobjParams"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static object RunInstanceMethod(System.Type t, string strMethod, 
InBlock.gif            
object objInstance, object [] aobjParams) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            BindingFlags eFlags 
= BindingFlags.Instance | BindingFlags.Public | 
InBlock.gif                BindingFlags.NonPublic;
InBlock.gif            
return RunMethod(t, strMethod, 
InBlock.gif                objInstance, aobjParams, eFlags);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 运行自定义方法
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="t"></param>
InBlock.gif        
/// <param name="strMethod"></param>
InBlock.gif        
/// <param name="objInstance"></param>
InBlock.gif        
/// <param name="aobjParams"></param>
InBlock.gif        
/// <param name="eFlags"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        private static object RunMethod(System.Type t, string 
InBlock.gif            strMethod, 
object objInstance, object [] aobjParams, BindingFlags eFlags) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MethodInfo m;
InBlock.gif            
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                m 
= t.GetMethod(strMethod, eFlags);
InBlock.gif                
if (m == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
throw new ArgumentException("There is no method '" + 
InBlock.gif                        strMethod 
+ "' for type '" + t.ToString() + "'.");
ExpandedSubBlockEnd.gif                }

InBlock.gif                                
InBlock.gif                
object objRet = m.Invoke(objInstance, aobjParams);
InBlock.gif                
return objRet;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }
 // end of class
InBlock.gif

ExpandedBlockEnd.gif}
  //  end of namespace





转载于:https://www.cnblogs.com/netflu/archive/2005/08/26/223294.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值