2

Is it possible to use namespaces in dunitx in such a way that all test fixtures under a namespace are enclosed by one pair of setup/teardown routines? (Similar to the SetupFixture attribute in nunit, http://www.nunit.org/index.php?p=setupFixture&r=2.5.5).

I tried to use the following unit names/namespaces:

  • Tests.MyFixture.pas for the TMyFixtureInitializer class with the common setup and teardown methods

  • Tests.MyFixture.MyTestUnit1.pas and Tests.MyFixture.MyTestUnit2.pas for the actual test classes.

I ran into the following problems:

  • TMyFixtureInitializer.SetupFixture and TeardownFixture are not executed when the class itself does not contain any test routine.

  • After adding a dummy test in TMyFixtureInitializer, the SetupFixture and TeardownFixture routines are called, but after the tests in Tests.MyFixture.MyTestUnit1 and Tests.MyFixture.MyTestUnit2.

  • When I set the SetupFixture attribute on the constructor, and the TeardownFixture attribute on the destructor of TMyFixtureInitializer, they are executed before and after all tests, ignoring the namespaces altogether.

Tests.MyFixture.pas:

unit Tests.MyFixture;

interface
uses
  DUnitX.TestFramework;

type

  [TestFixture]
  TMyFixtureInitializer = class(TObject)
  public

    [SetupFixture]
    procedure SetupMyFixture;
    [TeardownFixture]
    procedure TeardownMyFixture;
  end;

implementation


{ TMyFixtureInitializer }

procedure TMyFixtureInitializer.SetupMyFixture;
begin
  Self.Log('initialize a lot of stuff ...');
end;

procedure TMyFixtureInitializer.TeardownMyFixture;
begin
  Self.Log('cleanup a lot of stuff ...');
end;

initialization
  TDUnitX.RegisterTestFixture(TMyFixtureInitializer);
end.

Tests.MyFixture.MyTestUnit1.pas:

unit Tests.MyFixture.MyTestUnit1;

interface
uses
  DUnitX.TestFramework;

type
  [TestFixture]
  TestClass1 = class(TObject) 
  public
    [Test]
    procedure Test;
  end;

implementation


{ TestClass1 }

procedure TestClass1.Test;
begin
end;

initialization
  TDUnitX.RegisterTestFixture(TestClass1);
end.

(Source of Tests.MyFixture.MyTestUnit2.pas is analogous).

Does anyone have an example of how to use namespaces to organize initialization and cleanup?

1 Answers1

0

Imho, it does not make sense to define a [TestFixture] without any tests... And: as far as I know, there is no support for namespaces in DUnitX. DUnitX just runs all the fixtures (and the tests) in a loop.

If you only need methods that run before all tests / after all tests, you could just do the following (instead):

Tests.MyFixture.pas:

unit Tests.MyFixture;

interface
// uses
  //DUnitX.TestFramework;

type

  TMyFixtureInitializer = class(TObject)
  public

    class procedure SetupMyFixture;
    class procedure TeardownMyFixture;
  end;

implementation

{ TMyFixtureInitializer }

class procedure TMyFixtureInitializer.SetupMyFixture;
begin
  //Self.Log('initialize a lot of stuff ...');
end;

class procedure TMyFixtureInitializer.TeardownMyFixture;
begin
  //Self.Log('cleanup a lot of stuff ...');
end;

initialization
  TMyFixtureInitializer.SetupMyFixture;
finalization
  TMyFixtureInitializer.TeardownMyFixture;
end.

But possibly, this solution is just too simple for your problem... I think, you want to have more than one namespace... to kind-of group them. Sorry, no idea for this.

Markus
  • 357
  • 2
  • 13