随着不断地建立TestCase,需要管理的测试方法也越来越多,一打开Nunit,就是一个大大的测试树,对自动化测试而言,也许每个方法的说明并不重要,但有时候想测试某一项功能时,如何准确地找到该方法绝对是件头疼的事。我希望对左边的每个树结点都有相应的方法说明以便于我进行查看和管理。

    先看看源码吧,我使用的是nunit2.2.7 的源码项目,在代码中,当我们单击左边的树结点时,右边会显示对应得类名和方法名,好,找到这段代码:

 

private void testTree_SelectedTestsChanged(object sender, SelectedTestsChangedEventArgs e)

 
{

                   
if (!IsTestRunning) 

                   
{

                        suiteName.Text 
= e.TestName;

                        statusBar.Initialize(e.TestCount, e.TestName );

                   }


              }



suiteName就是STOP按钮旁那个显示类名和方法名的label控件,它被赋予了事件参数里测试对象名,我欣喜地发现SelectedTestsChangedEventArgs居然有一个属性是Descripton,于是我把代码改成这样:

suiteName.Text = e.TestName + e.Description;

如何给Description赋值呢,我开始去找对应的地方,希望找到取方法特性的地方,把DescriptionAttribute的值赋给事件参数。然后发现下面这段代码:

 

private void tests_SelectedTestChanged(UITestNode test)

              
{

                   
if (SelectedTestsChanged != null

                   
{

              SelectedTestsChangedEventArgs args 
= new SelectedTestsChangedEventArgs(test.Name, test.CountTestCases(),test.Description);

                        SelectedTestsChanged(tests, args);

                   }


              }


              看来Description参数是由UITestNode对象传递来的,UITestNode实现了ITest接口,而Description正是其属性之一,经过不断地代码跟踪,我找到了给description属性赋值的地方,可是困惑也就在这里开始了。

    

protected override string GetFixtureDescription( Type fixtureType )

         
{

              
if ( parms.HasTestFixtureType )

              
{

                   Attribute fixtureAttribute 
=

                       Reflect.GetAttribute( fixtureType, parms.TestFixtureType, 
true );

 

                   
// Some of our tests create a fixture without the attribute

                   
if ( fixtureAttribute != null )

                       
return (string)Reflect.GetPropertyValue( 

                            fixtureAttribute, 

                            
"Description",

                            BindingFlags.Public 
| BindingFlags.Instance );

              }


 

              
return null;

         }


        
    TestCase BuildFrom(System.Reflection.MethodInfo method)是类AbstractFixtureBuilder用来根据mehtodinfo构造TestCase对象的,上面代码位于GenericTestFixtureBuilder中,覆写了基类AbstractFixtureBuilderGetFixtureDescription函数以提供真正的实现。这段代码的作用就是利用反射取出自定义特性对象中Description属性,问题好像一下就明朗了,那么只要我这样写应该就可以显示了吧:

[TestFixture(Description="用作简单测试的素材")]

              
public class SimpleTestCase

              
{

                  [TestAttribute(Description
="测试示例方法")]

                  
public void TestSample()

                  
{

                  }


}



可是当我把程序跑起来时,发现不对啊,显示的Description的值根本就不是我在特性里写的内容,于是我花了不少时间进行耐心的跟踪,希望找到原因,每次发现值到最后的那一步就不是我想要的值了,怎么回事呢?无意中,我查了一下事件参数SelectedTestsChangedEventArgs的类,发现其Description属性返回的值居然不是构造函数里的description参数,不是成员变量description的值,居然是testName!

该类的代码如下:

    

public class SelectedTestsChangedEventArgs : EventArgs 

     
{

         
private string testName;

         
private int count;

         
private string description=string.Empty;

 

         
public SelectedTestsChangedEventArgs(string testName, int count) 

         
{

              
this.testName = testName;

              
this.count = count;

              

         }


 

         
public SelectedTestsChangedEventArgs(string testName, int count,string description) 

         
{

              
this.testName = testName;

              
this.count = count;

              
this.description = description;

         }


 

         
public string TestName 

         
{

              
get return testName; }

         }


 

         
public int TestCount 

         
{

              
get return count; }

         }


 

         
public string Description 

         
{

              
get return this.testName; }

         }


}



    原来我是被NUnit给耍了一把,找到问题就好办了,改下Description这个属性:

    

 public string Description 

         
{

              
get return this.description; }

     }


       
    跑下程序,终于显示出我想要的结果,为了显示得好看点,我把原来的label换成了两个TextBox。最后的显示效果如下:

   在后面的文章中,我会对NUnit进行进一步的改造和整合^_^

posted on 2006-03-25 18:52  sharpedge  阅读(2172)  评论(5编辑  收藏  举报