如何打破依赖关系以启用单元测试

时间:2011-12-29 15:35:15

标签: unit-testing delphi dependencies dunit bold-delphi

我花了很多时间考虑单元测试。我至少买了Working Effectively with Legacy Code作为电子书。大多数都是有意义的,它似乎是一本关于单元测试旧代码的好书。但我认为我需要一个起点,因为我们的项目Attracs很大。另见关于单元测试的myrelrel question

应用程序有一个UML模型来定义类,属性和关系,并使用Bold for Delphi。在模型中的每次更改后,我们都会进行往返。这会自动为文件businessclasses.pas和BusinessClasses_Interface.inc中的方法生成声明。如果更改需要更改数据库,也会生成SQL脚本。这项工作多年来一直运作良好,但我们从未使用任何单元测试。

所以我添加了一个新的testproject,然后依赖会导致麻烦。 我得到了

  

[DCC错误] Attracs_Interface_Uses.inc(10):F1026找不到文件:'MsxSupport.dcu'

总结错误

AttracsTest.dpr使用
BusinessClasses.pas使用
BusinessClasses_Interface.inc使用
Attracs_Interface_Uses.inc

那么如何打破依赖链呢?

请注意,实际上文件要大得多。模型中有300多个类,businessClasses.pas有超过53000行代码...... 作为一个测试用例,我只有TPerson类,其方法是AddResponsibility。但你应该理解这个原则。

这是我的档案:

AttracsTest.dpr

program AttracsTests;
{$IFDEF CONSOLE_TESTRUNNER}
{$APPTYPE CONSOLE}
{$ENDIF}
uses
  Forms,
  TestFramework,
  GUITestRunner,
  TextTestRunner,
  BusinessClasses in '..\..\server\code\BusinessClasses.pas',
  TestBusinessClasses in 'TestBusinessClasses.pas',
  ArrayOfObject in '..\..\server\code\ArrayOfObject.pas';

{$R *.RES}

begin
  Application.Initialize;
  if IsConsole then
    TextTestRunner.RunRegisteredTests
  else
    GUITestRunner.RunRegisteredTests;
end.  

TestBusinessClasses.pas

unit TestBusinessClasses;

interface

uses
  TestFramework,
  ArrayOfObject,
  AttracsAttributes,
  AttracsDefs,
  atXMLObjModel,
  BoldAttributes
  BoldDBInterfaces,
  BoldDefs,
  BoldDeriver,
  BoldDomainElement,
  BoldElements,
  BoldSubscription,
  BoldSystem,
  BoldSystemRT,
  BusinessClasses,   // Trigger the dependency, but also contain info about the classes get and set methods for attributes.  
  Classes,
  Contnrs,
  SysUtils,
  XMLIntf,
  XMLObjModel,
  XMLParser;

type
  TestTPerson = class(TTestCase)
  strict private
    FPerson: TPerson;
  public
    procedure SetUp; override;
    procedure TearDown; override;
  published
    procedure TestAddResponsibility;
  end;

implementation

procedure TestTPerson.SetUp;
begin
  FPerson := TPerson.Create;
end;

procedure TestTPerson.TearDown;
begin
  FPerson.Free;
  FPerson := nil;
end;

procedure TestTPerson.TestAddResponsibility;
var
  ReturnValue: Boolean;
  aSession: TLogonSession;
  aDevType: TDevTypeDef;
  aMarketArea: TMarketArea;
begin
  // TODO: Setup method call parameters
  ReturnValue := FPerson.AddResponsibility(aMarketArea, aDevType, aSession);
  // TODO: Validate method results
end;

initialization
  // Register any test cases with the test runner
  RegisterTest(TestTPerson.Suite);
end.

Attracs_Interface_Uses

AttracsDefs,
atXMLObjModel,
XMLObjModel,
XMLParser,
Contnrs,
XMLIntf,
ArrayOfObject,
BoldDBInterfaces,
MsxSupport         // Line that compiler complain about

BusinessClasses_Interface.inc

(*****************************************)
(*      This file is autogenerated       *)
(*   Any manual changes will be LOST!    *)
(*****************************************)

{$IFNDEF BusinessClasses_Interface.inc}
{$DEFINE BusinessClasses_Interface.inc}

{$IFNDEF BusinessClasses_unitheader}
unit BusinessClasses;
{$ENDIF}

{$INCLUDE Attracs.inc} //PATCH

interface

uses
  // interface uses
  {$INCLUDE Attracs_Interface_Uses.inc} ,
  // interface dependencies
  // attribute classes
  AttracsAttributes,
  BoldAttributes,
  // other
  Classes,
  SysUtils,
  BoldDefs,
  BoldSubscription,
  BoldDeriver,
  BoldElements,
  BoldDomainElement,
  BoldSystemRT,
  BoldSystem;

type
  { forward declarations of all classes }
  TPerson = class;

  TPerson = class(TAmStateObject)
  public
    function AddResponsibility(aMarketArea: TMarketArea; aDevType: TDevTypeDef; aSession: TLogonSession): Boolean; 
  end;

function GeneratedCodeCRC: String;

implementation

uses
  // implementation uses
  {$INCLUDE Attracs_Implementation_Uses.inc} ,
  // implementation dependencies
  // other
  BoldGeneratedCodeDictionary;

{$ENDIF}

Businessclasses.pas

    (*****************************************)
    (*      This file is autogenerated       *)
    (*   Any manual changes will be LOST!    *)
    (*****************************************)

    unit BusinessClasses;

    {$DEFINE BusinessClasses_unitheader}
    {$INCLUDE BusinessClasses_Interface.inc}

    { Includefile for methodimplementations 
      Have concrete implementation of methods}
    {$INCLUDE Person.inc}

    // Some get and set methods fopr attributes in the class

    // attribute FirstName
    function TPerson._Get_M_FirstName: TBAString;
    begin
      assert(ValidateMember('TPerson', 'FirstName', 14, TBAString));
      Result := TBAString(BoldMembers[14]);
    end;

    function TPerson._GetFirstName: String;
    begin
      Result := M_FirstName.AsString;
    end;

    procedure TPerson._SetFirstName(const NewValue: String);
    begin
      M_FirstName.AsString := NewValue;
    end;

    procedure InstallBusinessClasses(BoldObjectClasses: TBoldGeneratedClassList);
    begin
      BoldObjectClasses.AddObjectEntry('Person', TPerson);
    end;

    var
      CodeDescriptor: TBoldGeneratedCodeDescriptor;

    initialization
      CodeDescriptor := GeneratedCodes.AddGeneratedCodeDescriptorWithFunc('BusinessClasses', InstallBusinessClasses, InstallObjectListClasses, GeneratedCodeCRC);
    finalization
      GeneratedCodes.Remove(CodeDescriptor);
    end.

person.inc

function TPerson.AddResponsibility(aMarketArea: TMarketArea; aDevType: TDevTypeDef; aSession: TLogonSession): Boolean;
var
  vOCL: String;
  vDevResponse: TDevResponsible;
begin
  vOCL := Format('DevResponsible.allinstances->select((devType.TypeName = ''%s'') and (marketArea.name = ''%s''))->first',
                         [aDevType.TypeName, aMarketArea.name]);
  vDevResponse := GetApplicationKernel.EvaluateExpressionAsDirectElement(vOCL) as TDevResponsible;

  if not Assigned(vDevResponse) then
    vDevResponse := GetApplicationKernel.CreateAMObject('DevResponsible') as TDevResponsible;

  if Assigned(vDevResponse) then
  begin
    vDevResponse.marketArea := aMarketArea;
    vDevResponse.devType := aDevType;
    vDevResponse.responsiblePers := self;
    NotifyModificationHistory(Now, aSession, Format('Responsible for %s marketarea: %s', [aDevType.TypeName, aMarketArea.Name]));
    Result := True;
  end
  else
    Result := False;
end;

1 个答案:

答案 0 :(得分:1)

我会做的事情:

  • 分支项目,以便所有更改都可以在安全的“沙箱”中完成
  • run cnWizards使用Cleaner(或类似工具)清除单位依赖关系
  • 将所有必需的单元添加到测试项目dpr中,以便记录它们,但库路径上的第三方库是众所周知的依赖项
  • 根据可用资源制定计划:搜索意外的依赖关系(用于更深入的分析)或低挂果(可以无风险地删除)。使用限制性构建脚本进行持续集成,这些脚本仅为所需的第三方库指定库路径可以提供很多帮助(无论何时开发人员引入新的依赖项,构建都会中断)
  • 不时将“安全”更改合并回主干并使用最新的主干版本开始新的迭代