4

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;
Community
  • 1
  • 1
philnext
  • 3,242
  • 5
  • 39
  • 62
  • 1
    I've never come across anything like this for Delphi. It's much easier in Python because of the dynamic nature of that language. – David Heffernan Apr 06 '12 at 10:14
  • Not exactly the same, but in a recent blog Jordi Corbilla presented an idea of unit testing using `TCustomAttributes`. See [dunit-and-tcustomattributes](http://thundaxsoftware.blogspot.se/2012/04/dunit-and-tcustomattributes.html). Instead of adding testing in comments, they are applied as attributes. – LU RD Apr 06 '12 at 10:42
  • @LURD This nice article is related, but there is still a lot of code to write: attributes must have a fixed set of parameters. – Arnaud Bouchez Apr 06 '12 at 12:18
  • 1
    Delphi is a statically typed language, and as such statically typed code and unit tests, using dUnit or something like it, are likely to take more than one line. Python is by its dynamic nature prone to 1 liners, delphi is not, killing the concept. even in python real unit tests are better than this concept. – Warren P Apr 06 '12 at 12:38
  • @WarrenP I don't understand. In DUnit you have 'one line' tests, no ? – philnext Apr 06 '12 at 13:02
  • @philnext Yes, but due to the Object Pascal syntax, it is easier to write and maintain real testing classes instead of some inlined commands. For a "result := i1+i2" test, it will pass, but for more sophisticated test, there is IMHO nothing but good OOP code writing. – Arnaud Bouchez Apr 06 '12 at 15:30
  • @ArnaudBouchez OK but I don't want to replace or stop using DUnit, I just want to add basic test, used for non regression, but also for code documentation. – philnext Apr 06 '12 at 16:26
  • Do you know of any docstring type system that can be used in Delphi for documentation purposes but is also available to the executing code at runtime? – David Heffernan Apr 06 '12 at 20:35
  • One line? Ha. One line plus the 30 around it that make a dUnit test into a valid unit that can be part of a unit test app. – Warren P Apr 07 '12 at 18:43
  • @WarrenP Sure, I would say 'one line test'. But you have the same "pb" with other test tools even with Python. – philnext Apr 08 '12 at 10:16

1 Answers1

2

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.

Arnaud Bouchez
  • 42,305
  • 3
  • 71
  • 159
  • Hello Arnaud. My question was not 'how to send results to a console' but more 'how to script tests in the functions header comments'. I'll edit my question... – philnext Apr 06 '12 at 09:41
  • 1
    @Arnaud I'm struggling to see how any of this is at all similar to Python doctest, but perhaps I'm missing something. – David Heffernan Apr 06 '12 at 10:10
  • @philnext This is not a true console, but a virtual console, which is used for the test purpose. You may write the content within the function header comments, then use DWS to run it (in case of DWS). But of course, you'll need to compile the test source code (using dcc.exe). For a source parser to extract the headers, see e.g. [the castalia parser](https://github.com/jacobthurman/Castalia-Delphi-Parser). – Arnaud Bouchez Apr 06 '12 at 12:14