EaBIM一直以来积极响应国家“十二五”推进建筑业信息化的号召,对建筑领域的信息技术开展深入技术交流和探讨!致力于打造“BIM-建筑师-生态技术”三位一体综合资源交流共享平台,希望为BIM与可持续设计理念及技术的普及做出微小的贡献!!!

萧闫子 发表于 2014-1-10 14:34:08

[开发管理] vs2005 的单元测试

使用 Visual Studio 对公共方法进行单元测试,跟以前 NUnit 一样,都是引用需要的类,然后对公共方法进行测试。  但是 Visual Studio 还可以对类的私有方法进行测试。而Visual Studio 不允许测试代码跟实际代码放在一个项目中,我们来看看是Visual Studio UnitTest如何做的。  比如我们有这样一个私有方法
http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gifnamespace ClassLibrary1{
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif public
class DivisionClass{
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif  private
int Divide_private(int numerator, int denominator){
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif   return numerator / denominator;
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif  }
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}

  我们只要在这个私有方法的右键菜单中选择 创建单元测试,系统就自动产生了这个私有方法的单元测试代码。  下面我们来分析产生的单元测试代码,看Visual Studio UnitTest 是如何对私有方法进行单元测试的  简单来说,Visual Studio UnitTest 生成私有方法的单元测试时,将自动创建一个私有访问器。私有访问器是测试方法用于访问私有代码的方法。单元测试生成对私有访问器的调用,然后通过私有访问器来调用私有方法。私有访问器驻留在测试项目中的文件中;因此将被编译为测试项目程序集。  具体来看测试项目:  首先我们可以看到一个名为 VSCodeGenAccessors.cs 的新文件被创建,  这个文件包含两个类:  internal 类型的 BaseAccessor 类 和 派生自它的 ClassLibrary1_DivisionClassAccessor 类  BaseAccessor 类是通用的访问器基类。  ClassLibrary1_DivisionClassAccessor 类 则是对你要访问类的私有方法进行了反射封装,这样你就可以通过操作这个类来操作该私有方法了。如下面代码:

http://www.cppblog.com/Images/OutliningIndicators/None.gifusing Microsoft.VisualStudio.TestTools.UnitTesting;
http://www.cppblog.com/Images/OutliningIndicators/None.gif
http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gifnamespace TestProject1{
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif 
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif "Microsoft.VisualStudio.TestTools.UnitTestGeneration","1.0.0.0")]
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif internal
class BaseAccessor {
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif  protected Microsoft.VisualStudio.TestTools.UnitTesting.PrivateObject m_privateObject;
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif  protected BaseAccessor(object target, Microsoft.VisualStudio.TestTools.UnitTesting.PrivateType type) {
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif   m_privateObject =
new Microsoft.VisualStudio.TestTools.UnitTesting.PrivateObject(target, type);
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif  }
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif  protected BaseAccessor(Microsoft.VisualStudio.TestTools.UnitTesting.PrivateType type) :
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif  this(null, type) {}
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif  internal
virtual
object Target {
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif   get
{
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif    return m_privateObject.Target;
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif   }
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif  }
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif  public
override
string ToString() {
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif   return
this.Target.ToString();
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif  }
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif  public
override
bool Equals(object obj) {
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif   if (typeof(BaseAccessor).IsInstanceOfType(obj)) {
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif    obj = ((BaseAccessor)(obj)).Target;
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif   }
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif   return
this.Target.Equals(obj);
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif  }
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif  public
override
int GetHashCode() {
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif   return
this.Target.GetHashCode();
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif  }
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif 
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif "Microsoft.VisualStudio.TestTools.UnitTestGeneration","1.0.0.0")]
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif internal
class ClassLibrary1_DivisionClassAccessor : BaseAccessor {
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif protected
static Microsoft.VisualStudio.TestTools.UnitTesting.PrivateType m_privateType =
new Microsoft.VisualStudio.TestTools.UnitTesting.PrivateType(typeof(global::ClassLibrary1.DivisionClass));
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif internal ClassLibrary1_DivisionClassAccessor(global::ClassLibrary1.DivisionClass target) :
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif base(target, m_privateType) {}
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif internal
int Divide_private(int numerator, int denominator) {
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif  object[] args =
new
object[] {
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif   numerator,
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif   denominator};
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif  int ret = ((int)(m_privateObject.Invoke("Divide_private", new System.Type[] {
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif   typeof(int),
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif   typeof(int)}, args)));
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif   return ret;
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif  } 
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}
http://www.cppblog.com/Images/OutliningIndicators/None.gif
http://www.cppblog.com/Images/OutliningIndicators/None.gif
  注意:当您更改正在测试的代码文件中的私有方法时,这个访问器可能无法正常工作,需要重新生成专用访问器(ClassLibrary1_DivisionClassAccessor 类)。  整理一下就是:  这个访问器,是通过反射的方式实现的。  VSUT利用自动代码生成技术,在单元测试项目中先来给你要测试的类生成一个名字叫XXXAccessor的访问器。  这个访问器会把需要测试类的需要测试的私有方法暴露出来,这种方式,无论是私有还是公共的属性和方法。这种方式可以很方便的给测试方法搭建测试环境,MOCK对象的注入也容易了。在测试调用的时候,就简单的只有下面的代码了:
http://www.cppblog.com/Images/OutliningIndicators/None.gif"ClassLibrary1.dll")]
http://www.cppblog.com/Images/OutliningIndicators/None.gif
http://www.cppblog.com/Images/OutliningIndicators/None.gifpublic
void Divide_privateTest()
http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif{
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif DivisionClass target =
new DivisionClass();
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif TestProject1.ClassLibrary1_DivisionClassAccessor accessor =
new  TestProject1.ClassLibrary1_DivisionClassAccessor(target);
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif int numerator =
4;
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif int denominator =
0;
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif int expected =
0;
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif int actual;
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif actual = accessor.Divide_private(numerator, denominator);
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif Assert.AreEqual(expected, actual, "ClassLibrary1.DivisionClass.Divide_private 未返回所需的值。");
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif Assert.Inconclusive("验证此测试方法的正确性。");
http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}
http://www.cppblog.com/Images/OutliningIndicators/None.gif


Visual Studio 2005 新增的测试功能使我们的开发工作更为快捷,代码质量在一定程度上有所提高。想必大家对类的单元测试已经比较熟悉,这里我们来看看它提供的另一项更为有趣的测试——用户界面自动化测试。事实上这个测试的本质仍是单元测试,单元测试的各种方法在用户界面测试中仍然可用。  我们以一个简单的 Windows Form 应用程序为例(在 Visual Studio 2005 Team System 下演示,其它版本亦可参考)。如图所示,这是一个一元二次方程的求解程序。http://www.7dspace.com/files/pic/200602/07220458164.jpg  在 Visual Studio 2005 中加载这个程序的工程,切换到待测试窗体(Form1)的源代码视图。添加一个用户界面测试最简单的方法是在事件函数声明代码上右击鼠标,在快捷菜单中点击“Create Unit Tests”。在弹出的对话框中点选待测试的函数(特别是事件函数),确定。当然,手工添加一个单元测试项目也是可以的,但通过快捷菜单自动生成的代码中包含了一些测试辅助类和对象的声明,使用起来更加方便。  本例中,我们由 btnWorkOut_Click 创建单元测试,自动生成的代码如下:


public void btnWorkOut_ClickTest()
{
 Form1 target = new Form1();
 MyTestProject.XandY_Form1Accessor accessor = new MyTestProject.XandY_Form1Accessor(target);
 object sender = null; // TODO: Initialize to an appropriate value
 EventArgs e = null; // TODO: Initialize to an appropriate value
 accessor.btnWorkOut_Click(sender, e);
 Assert.Inconclusive("A method that does not return a value cannot be verified.");
}  熟悉单元测试的朋友应该明白 属性的含义。在这个测试函数中,target 是待测试窗体的一个实例。sender 和 e 是各个事件函数必备的参数,一般情况下取 null 即可。最重要的是 accessor 对象,它是我们进行用户界面自动化测试的“总代理”。通过它可以访问到待测试窗体的各个方法、属性和控件,并可调用控件已声明的各个事件。由此,我们可以用 accessor 模拟使用者通过键盘、鼠标对用户界面的各项操作,同时即时获知程序中各个变量的状态。有兴趣的朋友可以读读 XandY_Form1Accessor 类的定义,了解这个“总代理”工作的奥秘。  我们对测试代码进行以下修改,以便对程序运行的正确性进行测试。


public void btnWorkOut_ClickTest()
{
 Form1 target = new Form1();
 MyTestProject.XandY_Form1Accessor accessor = new MyTestProject.XandY_Form1Accessor(target);
 object sender = null; // TODO: Initialize to an appropriate value
 EventArgs e = null; // TODO: Initialize to an appropriate value
 target.Show();
 target.Refresh();
 System.Threading.Thread.Sleep(1000);
 accessor.textBox1.Text = "2";
 accessor.textBox2.Text = "4";
 accessor.textBox3.Text = "2";
 target.Refresh();
 System.Threading.Thread.Sleep(1000);
 accessor.btnWorkOut_Click(sender, e);
 target.Refresh();
 Assert.IsTrue(accessor.d >= 0);
 Assert.IsNotNull(accessor.xx);
 Assert.IsNotNull(accessor.xy);
 Console.WriteLine("X1 = " + accessor.textBox4.Text);
 Console.WriteLine("X2 = " + accessor.textBox5.Text);
 System.Threading.Thread.Sleep(1000);
 target.Close();
}  以上代码中,我们通过 target.Show() 呈现窗体,通过 target.Refresh() 可以即时地刷新窗体内容在屏幕的显示,通过 System.Threading.Thread.Sleep(1000) 来延时。这几段代码都不是必需的,加入它们是为了便于程序反应和测试者观察,我们也完全可以在不显示用户界面的情况下更快地运行自动测试。至于用 Assert 来监控变量状态和用 Console 做输出,用过单元测试的朋友都应该了解。  此时,我们运行这个测试,就会在屏幕上先后看到窗体被加载,三个文本框被填入数据,计算结果出现在下面的文本框中,最后窗体被关闭。  再来看看测试结果的输出,符合我们的预期。  Visual Studio 2005 的用户界面自动化测试就是这么简单。对于一般的测试,它使测试人员不必深究应用程序底层的实现。当然,对于本例这样简单的程序,我们用不着这么麻烦的测试,但是对于用户界面更加复杂的程序,操作上存在更多的组合方式,手工测试的繁琐程度以及查找问题的难度便大大上升了。使用 Visual Studio 2005 提供的自动化测试手段将在很大程度上减轻测试人员的负担。有兴趣的朋友甚至可以在这个基础上进行二次开发,引入自动化测试脚本等专业测试方案,实现通用性更强的用户界面自动化测试

页: [1]
查看完整版本: [开发管理] vs2005 的单元测试