從單元測試NUnit中理解.NET自定義屬性的應用[4] 單元測試工具
關鍵字:.NET NUnit
而InvokeXXX()函數則利用反射機制運行相關函數,可以看看以下幾個代碼段:
//From TemplateTestCase in NUnit.Core namespace
private void InvokeSetUp()
{
MethodInfo method = FindSetUpMethod(fixture);//取得[SetUp]標記的函數反射實例
if(method != null)
{
InvokeMethod(method, fixture);//運行該函數
}
}
FindSetUpMethod(…)通過調用一個叫FindMethodByAttribute(…)的函數,利用反射機制來獲得可調用該函數的MethodInfo,并最后通過InvokeMethod(MethodInfo,…)來運行。
//From Test class in NUnit.Core namespace
protected void InvokeMethod(MethodInfo method, object fixture)
{
if(method != null)
{
try
{
method.Invoke(fixture, null);//調用由method實例反射的方法或構造函數
}
catch(…)
//…
}
文章來源于領測軟件測試網 http://www.kjueaiud.com/