.NET 开源项目介绍及资源推荐:单元测试

说到单元测试,大家首 先会想到NUnit ,但对于NUnit 来说 还存在着一些不足,比如说它不能够与VS.NET IDE 集成、扩展能力比较差等,除此之外我们还 需要一些自动化的测试工具,本文将简单介绍一下如下几种单元测试工具以及推荐一些学习资源。

1 NUnit

2 TestDriven.Net

3 NUnitForms

4 NUnitAsp

 

一.NUnit

提起大名鼎鼎的NUnit ,我想没有几个不知道吧?NUnit 是一个 专门针对于.NET 的单元测试框架。在这之前有针对JavaJUnit, 针对C++CPPUnit ,它们都是属于xUnit 家族。 NUnitxUnit 家族种的第4 个主打产品, 完全由C# 语言来编写, 并且编写时充分利用了许多.NET 的特性, 比如反射, 客户属性等等,最重要的一点是它适合于所有.NET 语 言。

编写一段简单的单元测 试代码:

 

[TestFixture]
public   class  TestDatabase
{   
    [Test]
    
public   void  TestWithDebugger()
    
{
        ClassA ca 
=   new  ClassA();
        
int  expected  =   3 ;
        
int  result  =  ca.GetResult();
        Assert.AreEqual(expected,result);
    }

}

NUnit的使用非常简单,运行 后如下图所示:

NUnit 虽然在使用上非常简单,赢得了一部分用户,甚至于微软在企业库的源码中也提供了使用NUnit 测试的版本,但是NUnit 让我最不爽的地方 是没有提供Visual Studio IDE 插件,这样如果做单元测试,会发现自己每天忙于奔波 于IDENUnit GUI 之间,不停的 在切换窗口;还有一点NUnit 虽然跟JUnit 是 兄弟,但是它的威望和能力相较Junit 就差远了,尤其是扩展能力,后面我会介绍到一个NUnitWinForm 插件。

官方主页:http://www.nunit.org/

学习资源

1 NUnit官方文档

2 .园子里LIVENUnit详细使用方法

3 Aero实践单元测试(3)-Using NUnit

个人认为,看以上两篇 中文资料足以学会NUnit 的使用,它的难点在于测试用例的编写上,而不是工具本身的使用。


二.TestDriven.Net

TestDriven.Net 列在开源项目介绍里面其实有些不太合适,因为它现在已经是一种商业化的工 具,只有个人版可以免费下载使用,个人认为在开发中个人版的功能已经足够了,之所以我要把它放在这里介绍,是因为它太优秀了。前面说的NUnit ,虽然使用非常简单,但是它不能与我们的.NET 开 发环境集成起来,而TestDriven.Net 就是这样一款以插件的形式集成在Visual Studio 中的单元测试工具,它的前身是NUnitAddIn , 由Jamie Cansdale 大师开发,一开始作者只是想做一个NUnit 插件集成到Visual Studio 中,经 过多次版本更新,NUnitAddIn20049 月底更名为TestDriven.NET ,并在当年 的12 月初发布了它的第一个Release 版 本,即TestDriven.NET 1.0 。在今年的10 月 底,终于迎来了TestDriven.NET 2.0 版本的发布,最新的2.0 版本集成了.NET ReflectorNConverNConverExplorerTypeMock.NET 等,功能更加强大,我们还是使用前面的例子:

[TestFixture]
public   class  TestDatabase
{   
    [Test]
    
public   void  TestWithDebugger()
    
{
        ClassA ca 
=   new  ClassA();
        
int  expected  =   3 ;
        
int  result  =  ca.GetResult();
        Assert.AreEqual(expected,result);
    }

}

它所有的操作都是通过IDE 中 的右键菜单完成,如下图所示:

由于跟IDE 的集成,可以使我们很快的定位到出错的代码行:

官方主页:http://www.testdriven.net/

学习资源

由于TestDriven.Net 的使用非常简单,所以目前基本没有什么中文文章介绍,大家可以参考一下官方网站上 的QuickStart 以 及我在0612 月份《程序员》杂志上写的 一篇关于TestDriven.Net 的文章。

 

三.NunitForms

NUnitForms 从命名上看,就知道它跟NUnit 有关,没 错,它是NUnit 的一个WinFrom 的 扩展。它为Windows Forms 应用程序提供单元测试和压力测试, 可以非常容易的用它为你的Windows Forms 类 进行自动化测试,它提供了一个Recorder Application ,来记录你的操作。我们编 写类似于如下代码片断的测试代码:

ButtonTester button  =   new  ButtonTester( " buttonName " " formName " ); 

ControlTester textBox 
=   new  ControlTester( " nameOfSomeTextBox " );

Assertion.AssertEquals(
" defaultText " , textBox[ " Text " ];

textBox[
" text " =   " newText " ;

或者类似于这样的代码进行操作记录:

// records button.Click() 
public   void  Click( object  sender, EventArgs args)
{
    listener.FireEvent(TesterType, sender, 
" Click " );
}


// records: comboBox.Enter("text"); 
public   void  TextChanged( object  sender, System.EventArgs e)
{
    listener.FireEvent(TesterType, sender, 
" Enter " , ((ComboBox)sender).Text);
}


// records: comboBox.Select(3);  // text of item 3 
public   void  SelectedIndexChanged( object  sender, System.EventArgs e)
{
    EventAction action 
=   new  EventAction( " Select " , ((ComboBox)sender).SelectedIndex);
    action.Comment 
=  ((ComboBox)sender).Text;
    listener.FireEvent(TesterType, sender, action);
}

对于NUnitForms ,它 还有一个兄弟工具叫NUnitASP

官方主页:http://nunitforms.sourceforge.net/

学习资源

对于NUnitForms ,仍然是没有发现有好的中文资源,大家可以参考官方文档

 

四.NUnitAsp

NUnitAsp 可以说是NUnitForms 的兄弟,它也是一 个NUnit 的扩展,用来自动测试ASP.NET 页 面。虽然NunitAsp 可以完成一些ASP.NET 页 面的自动化测试工作,但是在编写测试用例的时候,如果界面上的元素比较多,编写起来会非常的麻烦,这也是为什么NunitAsp 一 直处于大紫不红的原因。它可以编写如下代码片断的测试代码:

public   void  TestLayout()
{
    TextBoxTester name 
=   new  TextBoxTester( " name " , CurrentWebForm);
    TextBoxTester comments 
=   new  TextBoxTester( " comments " , CurrentWebForm);
    ButtonTester save 
=   new  ButtonTester( " save " , CurrentWebForm);
    DataGridTester book 
=   new  DataGridTester( " book " , CurrentWebForm);

    Browser.GetPage(
" http://localhost/GuestBook/GuestBook.aspx " );

    AssertVisibility(name, 
true );
    AssertVisibility(comments, 
true );
    AssertVisibility(save, 
true );
    AssertVisibility(book, 
false );
}


public   void  TestSave()
{
    TextBoxTester name 
=   new  TextBoxTester( " name " , CurrentWebForm);
    TextBoxTester comments 
=   new  TextBoxTester( " comments " , CurrentWebForm);
    ButtonTester save 
=   new  ButtonTester( " save " , CurrentWebForm);
    DataGridTester book 
=   new  DataGridTester( " book " , CurrentWebForm);

    Browser.GetPage(
" http://localhost/GuestBook/GuestBook.aspx " );

    name.Text 
=   " Dr. Seuss " ;
    comments.Text 
=   " One Guest, Two Guest! Guest Book, Best Book! " ;
    save.Click();
}

官方主页:http://nunitasp.sourceforge.net/index.html

学习资源

跟自己的兄弟NUnitForms 一样,仍然没有好的中文文档,有兴趣的朋友可以参考‘NUnitAsp官方文档 ,相对来说还是比较全 的,有很多的教程。

 

总结

对于单元测试工具,就 简单的介绍这么多,我个人还是推荐使用TestDriven.Net 的个人版,至于后面两个NUnit 的扩展,大家可以参考一下,在实际开发中用它们来测试会很麻烦,至少我目前不敢去做这个尝试。还有 一个非常值得推荐的单元测试工具Mbunit ,有兴趣的朋友可以关注一下,我对它了解并不多。

本来想在文章中再介绍 一下开源的Mock 框架的,感觉太多了,只好单独放一篇文章来介绍了。

作者:Angelo Lee
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
原文地址:https://www.cnblogs.com/yefengmeander/p/2887766.html