Trying to get DUnit2 to add some integration tests from files. Currently I have got DUnit2 to file the files and add tests accordingly. However, the tests are in this structure:

Trying to get DUnit2 to add some integration tests from files. Currently I have got DUnit2 to file the files and add tests accordingly. However, the tests are in this structure:

+ My.exe
+-+ Integration Tests
  +-+ File Name
    +- Test Procedure Name

What I would like to do is for the test to run under "File Name" and not have the extra level of "Test Procedure Name". I have tried for the last couple of days without any success :(

Is this possible with DUnit2?

Comments

  1. This is the code that does layout that includes an extra level.

    ---- Start ----
    type
      TIntegrationTest = class(TTestCase)
      private
        FFileName : string;
      protected
      public
        constructor Create(const AFileName : string); override;
      published
        procedure ShouldSolvePuzzle;
      end;

    ...

    procedure AddIntegrationTests();
    const
      cINTEGRATION = 'Integration';
      cLEVELS_DIRECTORY = '..\..\Levels\';
    var
      LFileList: TStringDynArray;
      LFileName: string;
      LLevel: string;
      LTestCase : TTestCase;
      LTestProject : ITestProject;
      LTestSuite: ITestSuite;
    begin
      LTestProject := TestFramework.Projects();

      if Assigned(LTestProject) then
        LTestProject := TestFramework.TestProject((LTestProject.Manager as IProjectManager).FindProjectID(cINTEGRATION));

      if not Assigned(LTestProject) then
      begin
        LTestProject := TTestProject.Create(cINTEGRATION);
        RegisterProject(LTestProject);
      end;

      LFileList := TDirectory.GetFiles(cLEVELS_DIRECTORY,
                                       '*.txt',
                                       TSearchOption.soAllDirectories,
                                       function(const Path: string; const SearchRec: TSearchRec): Boolean
                                       begin
                                         Result := (SearchRec.Size <> 0);
                                       end);

      for LFileName in LFileList do
      begin
        LLevel := TPath.GetFileName(TPath.GetDirectoryName(LFileName));

        LTestSuite := LTestProject.SuiteByTitle(LLevel);

        if not Assigned(LTestSuite) then
        begin
          LTestSuite := TTestSuite.Create(string(LLevel));
          LTestProject.AddTest(LTestSuite);
        end;

        LTestCase := TIntegrationTest.Create(LFileName);
        LTestSuite.AddTest(LLevel + ' : ' + TPath.GetFileNameWithoutExtension(LFileName), LTestCase);
      end;
    end;

    initialization
      AddIntegrationTests()
    ----- End -----

    I have tried a number of things to remove the extra level, which failed for one reason or another, like:

    * Using TTestProc wasn't being registered as a test because the owner method wasn't the same class as the test method,

    * Was registering as a test but was coming up with a warning because no check calls were done - there are plenty of them but DUnit2 was checking the test case owner and not the test itself.

    * Got the layout I want, as a registered as a test but doesn't get executed and there is no name against it (code below):

    ---- Start ----
      TTestFrameworkDirectoryIterator = class(TTestCase)
      private
        FDirectory : string;
        FFileName : string;
        constructor Create(const AFileName : string; const ATestCase : ITestCase); reintroduce; overload;
        procedure CreateTests;
        procedure SolvePuzzle;
      protected
        function get_DisplayedName: string; override;
      public
        constructor Create(const ADirectory : string); reintroduce; overload;
      end;

    // ...

    constructor TTestFrameworkDirectoryIterator.Create(const ADirectory: string);
    begin
      inherited Create;
      FDirectory := ADirectory;
      CreateTests;
    end;

    constructor TTestFrameworkDirectoryIterator.Create(const AFileName: string; const ATestCase: ITestCase);
    begin
      inherited Create(CreateTests,
                       'aa',
                       SolvePuzzle,
                       TPath.GetFileName(TPath.GetDirectoryName(AFileName)) + ' : ' + TPath.GetFileNameWithoutExtension(AFileName),
                       ATestCase);

      FFileName := AFileName;
    end;

    procedure TTestFrameworkDirectoryIterator.CreateTests;

    ReplyDelete

  2. var
      LFileList: TStringDynArray;
      LFileName: string;
    begin
      LFileList := TDirectory.GetFiles(FDirectory,
                                       '*.txt',
                                       TSearchOption.soAllDirectories,
                                       function(const Path: string; const SearchRec: TSearchRec): Boolean
                                       begin
                                         Result := (SearchRec.Size <> 0);
                                       end);

      for LFileName in LFileList do
        AddTest(TTestFrameworkDirectoryIterator.Create(LFileName, Self));
    end;

    function TTestFrameworkDirectoryIterator.get_DisplayedName: string;
    begin
      Result := TPath.GetFileName(FDirectory);
    end;

    procedure AddIntegrationTests();
    const
      cINTEGRATION = 'Integration';
      cLEVELS_DIRECTORY = '..\..\Levels\';
    var
      LDirectories: TStringDynArray;
      LDirectory: string;
      LIntegrationProject : ITestProject;
      LTestCase : TTestCase;
      LTestProject : ITestProject;
    begin
      LTestProject := TestFramework.Projects();

      if Assigned(LTestProject) then
        LIntegrationProject := TestFramework.TestProject((LTestProject.Manager as IProjectManager).FindProjectID(cINTEGRATION));

      if not Assigned(LIntegrationProject) then
      begin
        LIntegrationProject := TTestProject.Create(cINTEGRATION);
        RegisterProject(LIntegrationProject);
      end;

      LDirectories := TDirectory.GetDirectories(cLEVELS_DIRECTORY);

      for LDirectory in LDirectories do
      begin
        LTestCase := TTestFrameworkDirectoryIterator.Create(LDirectory);
        LIntegrationProject.AddTest(LTestCase);
      end;
    end;

    initialization
      AddIntegrationTests()

    ----- End -----

    ReplyDelete

Post a Comment