Can I pass type parameters to a testcase in DUnitx?

1k views Asked by At

I'm writing testcases for my FastCode project.

I've written a generic tester like so:

  TTest<T> = record
  private
    class var Def: System.Generics.Defaults.IComparer<T>;
    class var F: FastDefaults.TComparison<T>;
  strict private
    class function Slow(const Left, Right: T): integer; static;
    class function Fast(const Left, Right: T): integer; static;
    class constructor Init;
  public
    class procedure Test(const Left, Right: T); static;
  end;

A typical test case looks like:

[Test]
[TestCase('Single', '100.0,100.0')]
...many more testcases...
procedure TestSingle(const L,R: Single);
[Test]
[TestCase('Double', '100.0,100.0')]
...many more testcases... (identical to the one above).
procedure TestDouble(const L,R: double);

The testing code is typically as follows (repeated for every single type):

procedure TestDefault.TestSingle(const L, R: Single);
begin
  TTest<Single>.Test(L,R);
end;

What I would like to do:

[Test]
[TestTypes('single,double,extended')]
[TestCase('description', '100.0,100.0')]
...many more test cases....
procedure Test<TC>(L,R: TC);

And have the test run for the types stated, so that I don't have to write so much boilerplate.
Can something like this be done in DUnitX?

1

There are 1 answers

0
Wosi On BEST ANSWER

Try to make your test class generic and register your test class with every single concrete type you want to test.

  [TestFixture]
  TMyTestObject<T> = class(TObject)
  public
    // Sample Methods
    // Simple single Test
    [Test]
    procedure Test1;
    // Test with TestCase Atribute to supply parameters.
    [Test]
    [TestCase('TestA','1,2')]
    [TestCase('TestB','3,4')]
    procedure Test2(const AValue1 : T; const AValue2 : T);
  end;

  //...

  initialization
    TDUnitX.RegisterTestFixture(TMyTestObject<integer>);
    TDUnitX.RegisterTestFixture(TMyTestObject<double>);