如何在CodeGear BDS 2007中创建和使用测试用例--DUNIT

  如何在Delphi 2007中使用敏捷开发方式?如何创建和使用测试用例?这些好象一直都是Java的专利。其实,从Delphi 8开始Borland公司开始引入JUNIT的兄弟--DUNIT了,有了DUNIT,再加上Together,世界终于不一样了。

  那么,我们如何在Delphi中使用DUNIT呢?
  如果你使用的是Delphi2005以前的Delphi版本,如Delphi 7/8,你需要自已动手写上测试用例,尽管有DUNIT的Demo,但也是很烦很累的。如果是用CodeGear BDS 2007的话,这一切又不同了,IDE中集成了DUNIT与Together不说,最关键是的加入自动化的Test Project和Test Case 向导。
  测试用例、测试用例关键在这个“例”上,“例”者,“类”也!也就是说,要测试的对象必须是用类封装起来的对象,所以我们得先进行类的定义,然后才能测试类中的方法。
  举个例子:
  一、文件构成:
    首先建立一项目组(Project Group),包含两个项目,
    项目一,要测试的项目:
           PTest.prj
             |--uMaint.pas
             |--frmMaint.dfm
             |--uXXXMath.pas

    项目二一,DUNIT项目:
           PTestTests.prj
             |--uXXXMath.pas(手动加入的项目一中的要测试的类文件)
             |--TestuXXXMath.pas

  二、创建过程
     1、项目一创建过程,略!
     其中uXXXMath.pas文件的内容为:

  1. unit uXXXMath;  
  2.   
  3. interface  
  4. uses SysUtils;  
  5.   
  6. type  
  7.   
  8. TXXXMath = class  
  9. public  
  10.   function getSum(const a,b:Integer):Integer;//求和函数  
  11. end;  
  12.   
  13. implementation  
  14.   
  15. { TXXXMath }  
  16.   
  17. function TXXXMath.getSum(const a, b: Integer): Integer;  
  18. begin  
  19.   Result := a+b;  
  20. end;  
  21.   
  22. end.  



    2、项目二(测试用例)的创建过程
        a)File|New|Other...,Unit Test-->Test Project,建立一个Test Project项目。
点击在新窗口中浏览此图片
点击在新窗口中浏览此图片
点击在新窗口中浏览此图片
        b)File|New|Other...,Unit Test-->Test Case,创建测试用户。
点击在新窗口中浏览此图片
点击在新窗口中浏览此图片
        这就是Delphi 2007为我们自动生成的测试用例文件的内容,无须任何修改即可使用了,当然我们在测试函数中还是给函数变量进行了赋值,并加入了一条对测试结果进行判断的语句:

  1. unit TestuXXXMath;  
  2. { 
  3.  
  4.   Delphi DUnit Test Case 
  5.   ---------------------- 
  6.   This unit contains a skeleton test case class generated by the Test Case Wizard. 
  7.   Modify the generated code to correctly setup and call the methods from the unit  
  8.   being tested. 
  9.  
  10. }  
  11.   
  12. interface  
  13.   
  14. uses  
  15.   TestFramework, SysUtils, uXXXMath;  
  16.   
  17. type  
  18.   // Test methods for class TXXXMath  
  19.     
  20.   TestTXXXMath = class(TTestCase)  
  21.   strict private  
  22.     FXXXMath: TXXXMath;  
  23.   public  
  24.     procedure SetUp; override;  
  25.     procedure TearDown; override;  
  26.   published  
  27.     procedure TestgetSum;  
  28.   end;  
  29.   
  30. implementation  
  31.   
  32. procedure TestTXXXMath.SetUp;  
  33. begin  
  34.   FXXXMath := TXXXMath.Create;  
  35. end;  
  36.   
  37. procedure TestTXXXMath.TearDown;  
  38. begin  
  39.   FXXXMath.Free;  
  40.   FXXXMath := nil;  
  41. end;  
  42.   
  43. procedure TestTXXXMath.TestgetSum;  
  44. var  
  45.   ReturnValue: Integer;  
  46.   b: Integer;  
  47.   a: Integer;  
  48. begin  
  49.   // TODO: Setup method call parameters  
  50.   a := 10;    //我们加上的  
  51.   b := 20;  
  52.   ReturnValue := FXXXMath.getSum(a, b);  
  53.   CheckEquals(a+b,ReturnValue,'结果不对呀!出错了?');//我们加上的  
  54.   
  55.   // TODO: Validate method results  
  56. end;  
  57.   
  58. initialization  
  59.   // Register any test cases with the test runner  
  60.   RegisterTest(TestTXXXMath.Suite);  
  61. end.  



  三、编译、运行测试用例项目,测试结果一目了然。
点击在新窗口中浏览此图片
  四、可能的问题
  1、Q:无法加入要测试的单元?
     A:要测试的函数或过程不是类成员。
  2、Q:运行测试项目文件后,主界面中的“执行”按钮不可用(灰色的)。
     A:系统自动加入的欲测试单元文件丢失(可能是Delphi2007中的一个BUG吧),请手动再加一次。

posted @ 2009-04-30 19:11  小宇飞刀  阅读(130)  评论(0编辑  收藏  举报