Question

Is there any equivalent of DocTest for Delphi. I use DUnit but I like the Python DocTest idea. I saw some answer like here but I think that, for simple functions, a DocTest like could be OK. My goal is to define my tests in the comment header when I write the function. Like :

function Plus(i1, i2 : integer) : integer;
//>>> Check( Plus(1, 3) = 4)
begin
  result := i1 + i2;
end;
Was it helpful?

Solution

The idea is that you can use a "console" to output some results to testing code, then compare the output text content to an expected value.

Take a look for instance at the regression tests available with the great DWSScript Open Source project. You'll find out some .pas files and some related .txt files.

For instance abs.pas:

var vf = 1.5;
var vi = 2;

var i : Integer := Abs(-vi);
PrintLn(i);
PrintLn(Abs(vi));

var f : Float := Abs(-vf);
PrintLn(f);
PrintLn(Abs(vf));

And the corresponding abs.txt content:

2
2
1.5
1.5

AFAIK there is no already existing solution by now integrate in the Delphi world.

Writing the test in the comment will lack for IDE auto-completion, and somewhat break the object pascal design. It would be something easy with DWS, but require to call the command-line Delphi compiler. Honestly, I do not see what is wrong having your own set of units dedicated to tests. A small piece of code with a for..to loop with fixed and random values will have a much better test coverage than a fixed set of parameters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top